1、一个类A有两个成员变量v、num,v有一个初值100。
定义一个方法guess,对A类的成员变量v,用num进行猜大小。
如果大了则提示大了,小了则提示小了。等于则提示猜测成功。
在main方法中测试。
import java.util.Scanner;
public class ClassPractice01 {
public int v=100;
public int num;
public void guess( int num){
this.num=num;
if(this.v>this.num){
System.out.println("你猜小了");
}else if(this.v==this.num ){
System.out.println("你猜对了");
}else {
System.out.println("你猜大了");
}
}
public static void main(String[] args) {
ClassPractice01 c=new ClassPractice01();
Scanner s=new Scanner(System.in);
System.out.println("请输入猜测数字");
c.guess(s.nextInt());
}
}
2、创建一个圆Circle类。为该类提供一个变量r表示半径,一个常量PI表示圆周率;
同时为该类提供两个方法:方法一用于求圆的面积,方法二用于求圆的周长;
为该类提供一个无参的构造方法,用于初始化r的值为4。
为该类提供一个有参的构造方法,用于初始化r的值。
在main方法中测试。
public class Circle02 {
public double r;
public double PI=3.1415926;
void area(){
System.out.println("半径为"+r+"圆的面积为:"+r*r*PI);
}
double perimeter(){
return 2*r*PI;
}
public Circle02() {
System.out.println("调用了无参构造函数");
}
public Circle02(int i) {
r=i;
System.out.println("调用了有参构造函数");
}
public static void main(String[] args) {
Circle02 c1=new Circle02();
c1.r=4;
c1.area();
System.out.println("半径为"+c1.r+"圆的面积为:"+c1.perimeter());
Circle02 c2=new Circle02(8);
c2.area();
System.out.println("半径为"+c2.r+"圆的面积为:"+c2.perimeter());
}
}
3、定义交通工具类型,控制速度
请定义一个交通工具(Vehicle)的类,其中有:
属性:速度(speed),车的类型(type)等等
方法:移动(move()),设置速度(setSpeed(double s)),加速speedUp(double s),减速speedDown(double s)等等.
最后在测试类Vehicle中的main()中实例化一个交通工具对象,
并通过构造方法给它初始化speed,type的值,并且打印出来。另外,调用加速,减速的方法对速度进行改变。
public class Vehicle03 {
public double speed;
public String type;
public void move(){
System.out.println("小车移动");
}
public void setSpeed(double s){
this.speed=s;
System.out.println("设置汽车速度为:"+this.speed);
}
public void SpeedUp(double s){
this.speed+=s;
System.out.println("加速后汽车速度为:"+this.speed);
}
public void speedDown(double s){
this.speed-=s;
System.out.println("减速后汽车速度为:"+this.speed);
}
public Vehicle03(){
}
public Vehicle03(int speed,String type){
this.speed=speed;
this.type=type;
System.out.println("÷速度为:"+this.speed+",类型为:"+this.type);
}
public static void main(String[] args) {
Vehicle03 car=new Vehicle03(20,"小车");
car.move();
car.setSpeed(40);
car.SpeedUp(10);
car.speedDown(50);
}
}
4、定义一个点类Point,包含2个成员变量x、y分别表示x和y坐标,
2个构造器Point()和Point(int x0,y0),以及一个movePoint(int dx,int dy)方法实现点的位置移动,
创建两个Point对象p1、p2,分别调用movePoint方法后,打印p1和p2的坐标。
public class Point04 {
int x;
int y;
public void movePoint(int x,int y){
this.x+=x;
this.y+=y;
}
public Point04(){
}
public Point04(int x,int y){
this.x=x;
this.y=y;
}
public static void main(String[] args) {
Point04 p1=new Point04(10,10);
p1.movePoint(-1,2);
System.out.println("p1坐标为("+p1.x+","+p1.y+")");
Point04 p2=new Point04(100,100);
p2.movePoint(1,2);
System.out.println("p2坐标为("+p2.x+","+p2.y+")");
}
}
5、定义一个矩形类Rectangle:
(1) 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。
(2)有2个属性:长length、宽width
(3)通过构造方法Rectangle(int width, int length),分别给两个属性赋值
(4) 创建一个Rectangle对象,并输出相关信息
public class Rectangle05 {
public double height;
public double width;
public double getArea(){
return this.height*this.width;
}
public double getPre(){
return (this.height+this.width)*2;
}
public void showAll(double area,double pre){
System.out.println("矩形宽为:"+this.width+",高为:"+this.height+",面积为:"+area+",周长为:"+pre);
}
public Rectangle05(){
}
public Rectangle05(int width,int height){
this.height=height;
this.width=width;
}
public static void main(String[] args) {
Rectangle05 rec=new Rectangle05(5,6);
rec.showAll(rec.getArea(),rec.getPre());
}
}
6、为“无名的粉”写一个类:class WuMingFen (注:“无名”是长沙一家很有特色的粉店)
要求:
(1).有三个属性:面码:String theMa 粉的分量(两):int quantity
是否带汤:boolean likeSoup
(2).写一个普通方法:check(),用于查看粉是否符合要求。即:将对象的三个属性打印在控制台上。
(3).写一个构造方法,以便于简化初始化过程,
如,叫一碗3两牛肉带汤的粉:
WuMingFen f1 = new WuMingFen(“牛肉”,3,true);
(4).重载构造方法,使得初始化过程可以多样化,
比如叫一碗2两鸡丝粉,则默认是带汤的:
WuMingFen f2 = new WuMingFen(“鸡丝”,2);
(5).如何顾客这样说:老板,一碗粉。则默认是酸辣面码、2两、带汤的?
WuMingFen f3 = new WuMingFen();
(6).调用check()方法,将上面每碗粉的属性都打印在控制台上。
public class WuMingFen06 {
public String theMa;
public int quantity;
public boolean likeSoup;
public void check(){
//判断likeSoup的值
if(this.likeSoup){
System.out.println("一碗"+this.quantity+"两"+this.theMa+"带汤的面");
}else{
System.out.println("一碗"+this.quantity+"两"+this.theMa+"不带汤的面");
}
}
public WuMingFen06(String theMa, int quantity,boolean likeSoup){
this.theMa=theMa;
this.quantity=quantity;
this.likeSoup=likeSoup;
}
public WuMingFen06(String theMa, int quantity) {
this(theMa,quantity,true);
}
public WuMingFen06() {
this("酸辣",2,true);
}
public static void main(String[] args) {
WuMingFen06 f1 = new WuMingFen06("牛肉",3,true);
f1.check();
WuMingFen06 f2 = new WuMingFen06("鸡丝",2);
f2.check();
WuMingFen06 f3 = new WuMingFen06();
f3.check();
}
}
7、设计一个BankAccount类,实现银行某账号的资金往来账目管理,包括建账号、存入、取出等。
BankAccount类包括,账号(BankAccountId)、开户日期Date(日期),Rest(余额)。
另有一个构造函数和三个成员函数Bankin()(处理存入账),Bankout()处理取出账)。
mport java.util.Scanner;
public class BankAccount07 {
public int bankAccountId;
public int date;
public int rest;
public void Bankin(int save) {
if (save >= 0 && save <= this.rest) {
this.rest += save;
System.out.println("账号:" + this.bankAccountId + ",开户日期为:" + this.date + ",存入后余额为" + this.rest);
} else {
System.out.println("请输入正确的数字");
}
}
public void Bankout(int take){
if(take>=0&&take<=this.rest){
this.rest-=take;
System.out.println("账号:"+this.bankAccountId+",开户日期为:"+date+",取出后余额为"+this.rest);
}else {
System.out.println("请输入正确的数字");
}
}
public BankAccount07(){}
public BankAccount07(int BankAccountId,int Date, int Rest){
this.bankAccountId=BankAccountId;
this.date=Date;
this.rest=Rest;
System.out.println("账号:"+this.bankAccountId+",开户日期为:"+this.date+",余额为"+this.rest);
}
public static void main(String[] args) {
BankAccount07 b1=new BankAccount07(1234,20210726,15000);
Scanner sin=new Scanner(System.in);
System.out.println("输入存入金额");
b1.Bankin(sin.nextInt());
System.out.println("输入取出金额");
b1.Bankout(sin.nextInt());
}
}
8、设计一个类Student,该类包括姓名、学号和成绩。
设计一个方法,对有5名学生按照成绩从高到低的顺序输出姓名、学号和成绩信息。
public class Student08 {
String name;
int id;
double score;
public Student08(){
}
public Student08(String name, int id, int score) {
this.name = name;
this.id = id;
this.score = score;
}
void sort(Student08[] student08s){
Student08 stu;
for (int i = 0; i <student08s.length-1 ; i++) {
for (int j=i+1;j<student08s.length;j++){
if( student08s[i].score<student08s[j].score){
stu=student08s[i];
student08s[i]=student08s[j];
student08s[j]=stu;
}
}
}
for (int i = 0; i <student08s.length ; i++) {
System.out.println(student08s[i].name+","+student08s[i].id+","+student08s[i].score);
}
}
public static void main(String[] args) {
Student08[] st=new Student08[5];//引用类型数组
st[0]=new Student08("张三",10,50);//每一个数组元素都是一个对象
st[1]=new Student08("张四",11,51);
st[2]=new Student08("张五",12,59);
st[3]=new Student08("张六",13,53);
st[4]=new Student08("张七",14,54);
//或者直接创建对象后将对象作为数组元素赋值
//创建对象调用对象排序方法
Student08 s=new Student08();
s.sort(st);
}
}
9、编写一个程序,输入N个学生数据,包括学号、姓名、成绩,要求只输出成绩在80~89分的学生数据。
public class InStudentScore09 {
String name;
int id;
double score;
void sort(InStudentScore09[] iss){
for(int i=0;i<iss.length;i++){
if(iss[i].score>80&&iss[i].score<89){
System.out.println(iss[i].name+"的成绩为"+iss[i].score+",学号为:"+iss[i].id);
}
}
}
public InStudentScore09(){}
public InStudentScore09(String name,int id, double score){
this.name=name;
this.id=id;
this.score=score;
}
public static void main(String[] args) {
//Scanner sc= new Scanner(System.in);
InStudentScore09[] iss=new InStudentScore09[5];
// for (int i = 0; i < iss.length; i++) {
// System.out.println("请输入学生信息");
// iss[i].name=sc.nextLine();
// iss[i].id=sc.nextInt();
// iss[i].score=sc.nextDouble();
// }
iss[0]=new InStudentScore09("张三",10,85);
iss[1]=new InStudentScore09("张四",11,51);
iss[2]=new InStudentScore09("张五",12,59);
iss[3]=new InStudentScore09("张六",13,53);
iss[4]=new InStudentScore09("张七",14,54);
InStudentScore09 in=new InStudentScore09();
in.sort(iss);
}
}
10、编写一个程序,输入N个学生数据,包括学号、姓名、成绩,求学生的成绩总和、平均值、最大值、最小值。
public class Student10 {
String name;
int id;
double score;
public Student10(){
}
public Student10(String name, int id, int score) {
this.name = name;
this.id = id;
this.score = score;
}
void JiSuan(Student10[] stu){
int sum=0;
for (int i = 0; i < stu.length ; i++) {
sum+=stu[i].score;
}
double avg=sum/stu.length;
Student10 a;
for (int i = 0; i <stu.length-1 ; i++) {
for (int j=i+1;j<stu.length;j++){
if( stu[i].score>stu[j].score){
a=stu[i];
stu[i]=stu[j];
stu[j]=a;
}
}
}
System.out.println("总成绩为"+sum+",平均成绩为"+avg+",最高分:"+stu[stu.length-1].score+",最低分:"+stu[0].score);
}
public static void main(String[] args) {
Student10[] st=new Student10[5];
st[0]=new Student10("张三",10,55);
st[1]=new Student10("张四",11,51);
st[2]=new Student10("张五",12,59);
st[3]=new Student10("张六",13,53);
st[4]=new Student10("张七",14,54);
Student10 stu=new Student10();
stu.JiSuan(st);
}
}
总结:
opp编程其实就是分析问题,判断需要哪些属性,方法,然后创建对象,使用对象调用属性和方法解决问题。