- 定義一個(gè)點(diǎn)類(lèi)Point,包含2個(gè)成員變量x楷力、y分別表示x和y坐標(biāo)喊式,2個(gè)構(gòu)造器Point()和Point(int x0,y0),以及一個(gè)movePoint(int dx,int dy)方法實(shí)現(xiàn)點(diǎn)的位置移動(dòng)孵户。編寫(xiě)一個(gè)程序,創(chuàng)建兩個(gè)Point對(duì)象p1岔留、p2夏哭,分別調(diào)研movePoint方法后,打印p1和p2的坐標(biāo)献联。
public class Point { private int x0; private int y0; public Point() { } public Point(int x0,int y0) { this.x0=x0; this.y0=y0; } public void movePoint(int dx,int dy){ this.x0 = this.x0 + dx; this.y0 = this.y0 + dy; } } public class Point { private int x0; private int y0; public Point() { } public Point(int x0,int y0) { this.x0=x0; this.y0=y0; } public void movePoint(int dx,int dy){ this.x0 = this.x0 + dx; this.y0 = this.y0 + dy; } }
- (1)定義一個(gè)矩形類(lèi)Rectangle:
1)定義三個(gè)方法:getArea()求面積方庭、getPer()求周長(zhǎng),showAll()分別在控制臺(tái)輸出長(zhǎng)酱固、寬、面積头朱、周長(zhǎng)运悲。
2)有2個(gè)屬性:長(zhǎng)length、寬width
3)通過(guò)構(gòu)造方法Rectangle(int width, int length)项钮,分別給兩個(gè)屬性賦值
public class Rectangle { int width; int height; public Rectangle() { } public Rectangle(int width,int height) { this.width=width; this.height=height; } public double getArea() { return this.width*this.height; } public double getPer() { return 2*(this.width+this.height); } public void showAll() { System.out.println("Width="+this.width); System.out.println("Height="+this.height); System.out.println("Area="+this.getArea()); System.out.println("Per="+this.getPer()); } }
- 定義一個(gè)筆記本類(lèi)班眯,該類(lèi)有顏色(char)和cpu型號(hào)(int)兩個(gè)屬性。
(1)無(wú)參和有參的兩個(gè)構(gòu)造方法烁巫;有參構(gòu)造方法可以在創(chuàng)建對(duì)象的同時(shí)為每個(gè)屬性賦值署隘;
(2) 輸出筆記本信息的方法
然后編寫(xiě)一個(gè)測(cè)試類(lèi),測(cè)試筆記本類(lèi)的各個(gè)方法亚隙。
public class ComputeTest { public static void main(String[] args) { Computer c1 = new Computer(); c1.showComputer(); Computer c2 = new Computer('紅', 32); c2.showComputer(); } } class Computer { private char color; private int cpuNum; public Computer() { } public Computer(char color, int cpuNum) { this.color = color; this.cpuNum = cpuNum; } public char getColor() { return color; } public void setColor(char color) { this.color = color; } public int getCpuNum() { return cpuNum; } public void setCpuNum(int cpuNum) { this.cpuNum = cpuNum; } public void showComputer() { System.out.println("筆記本的顏色:" + getColor()); System.out.println("筆記本的CPU型號(hào):" + getCpuNum()); } }
- 設(shè)計(jì)一個(gè)類(lèi)Student磁餐,該類(lèi)包括姓名、學(xué)號(hào)和成績(jī)阿弃。設(shè)計(jì)一個(gè)方法诊霹,按照成績(jī)從高到低的順序輸出姓名、學(xué)號(hào)和成績(jī)信息渣淳。
public class Student { private String name; private String number; private int score; public Student() { } public Student(String name, String number, int score) { this.name = name; this.number = number; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public void sortByScore(Student[] students){ int max= students[0].score; for(int i =0 ;i<students.length;i++){ for(int j = 0;j<students.length;j++){ if(students[i].getScore()>students[j].getScore()){ Student b = students[i]; students[i] = students[j]; students[j] = b; } } } System.out.println("按成績(jī)排序后的結(jié)果如下:"); for(int i = 0 ;i<students.length;i++){ System.out.print(students[i].getName() + " "+ students[i].getNumber()+" "+students[i].getScore()); System.out.println(); } } } public class StudentTest { public static void main(String[] args) { Student s1= new Student("Kitty","0000001",70); Student s2= new Student("Dingdang","0000002",85); Student s3= new Student("Tom","0000003",60); Student[] students = new Student[]{s1,s2,s3}; Student s = new Student(); s.sortByScore(students); } }
- (1)定義一個(gè)人類(lèi)Person:
1)定義一個(gè)方法sayHello()脾还,可以向?qū)Ψ桨l(fā)出問(wèn)候語(yǔ)“hello,my name is XXX”
2)有三個(gè)屬性:名字、身高入愧、體重
public class Person { String name; int age; double height; public void sayHello() { System.out.println("Hello"); } }
- (2)定義一個(gè)PersonCreate類(lèi):
1)創(chuàng)建兩個(gè)對(duì)象鄙漏,分別是zhangsan,33歲棺蛛,1.73怔蚌;lishi,44鞠值,1.74
2)分別調(diào)用對(duì)象的sayHello()方法媚创。
public class PersonCreate { public static void main(String[] args) { Person p = new Person(); p.name="zhangsan"; p.age=33; p.height=1.73; p.sayHello(); Person q = new Person(); q.name="lisi"; q.age=44; q.height=1.74; q.sayHello(); } }
- (1)定義一個(gè)人類(lèi)Person:
1)定義一個(gè)方法sayHello(),可以向?qū)Ψ桨l(fā)出問(wèn)候語(yǔ)“hello,my name is XXX”
2)有三個(gè)屬性:名字彤恶、身高钞钙、體重
3)通過(guò)構(gòu)造方法鳄橘,分別給三個(gè)屬性賦值
public class Constructor { public static void main(String[] args) { Person p=new Person("zhangsan",33,1.83); Person q=new Person("lisi",44,1.74); Person w = new Person(); } } class Person { String name; int age; double height; public Person(){ } public Person(String n,int a,double h) { name=n; age=a; height=h; } public void sayHello() { System.out.println("Hello, my name is "+name); } }
- (2)定義一個(gè)Constructor類(lèi):
1)創(chuàng)建兩個(gè)對(duì)象,分別是zhangsan芒炼,33歲瘫怜,1.73;lishi本刽,44鲸湃,1.74
2)分別調(diào)用對(duì)象的sayHello()方法。(答案:Constructor.java子寓、或者是Person3.java)
public class Person3 { private String name; private int age; private double height; public Person3(String name,int age) { this.name=name; //此處如果不用this會(huì)出現(xiàn)問(wèn)題 this.age=age; } public void sayHello() { System.out.println("Hello, my name is "+name); } public static void main(String args[]){ Person3 p = new Person3("a",1); p.sayHello(); } }
- 定義一個(gè)汽車(chē)類(lèi)Vehicle暗挑,要求如下:
(1)屬性包括:汽車(chē)品牌brand(String類(lèi)型)、顏色color(String類(lèi)型)和速度speed(double類(lèi)型)斜友,并且所有屬性為私有炸裆。
(2)至少提供一個(gè)有參的構(gòu)造方法(要求品牌和顏色可以初始化為任意值,但速度的初始值必須為0)鲜屏。
(3)為私有屬性提供訪(fǎng)問(wèn)器方法烹看。注意:汽車(chē)品牌一旦初始化之后不能修改。
(4)定義一個(gè)一般方法run()洛史,用打印語(yǔ)句描述汽車(chē)奔跑的功能
定義測(cè)試類(lèi)VehicleTest惯殊,在其main方法中創(chuàng)建一個(gè)品牌為“benz”、顏色為“black”的汽車(chē)也殖。
public class Vehicle { private String brand; private String color; private double speed; Vehicle(){ } Vehicle(String brand,String color){ this.brand = brand; this.color = color; speed = 0; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getSpeed() { return speed; } public void setSpeed(double speed) { this.speed = speed; } public void run(){ System.out.println(getColor()+"的"+getBrand()+"的速度是"+getSpeed()); } public String getBrand() { return brand; }