Java基础 第3章练习题 大外软件学院
创建主类:
创建两个机动车对象。创建第一个时调用无参数的构造方法,调用成员方 法使其车牌为“辽A9752”,并让其加速。
创建第二个时调用有参数的构造方法,使其车牌为“辽B5086”,车速为 150,载重为200,并让其减速。输出两辆车的所有信息。
4. class Vehicle{
String id; int speed; double load; void upSpeed(){ ++speed; }
void downSpeed(){ --speed; }
void idChanging(String i){ id = i; }
double showLoad(){ return load; }
Vehicle(){
id = \ speed = 100; load = 100; }
Vehicle(String i, int s, double l){ id = i; speed = s; load = l; } }
public class Four {
public static void main(String[] args) { Vehicle v1 = new Vehicle(); v1.idChanging(\辽A9752\ v1.upSpeed();
Vehicle v2 = new Vehicle(\辽B5086\ v2.downSpeed();
System.out.println(v1.id+\ System.out.println(v2.id+\ } }
5. 创建一个point类,有成员变量x、y,方法getX()、setX(),还有一个构造方法用于初
第9页 共13页
Java基础 第3章练习题 大外软件学院
始化x和y。创建主类来测试它。 5. class Point { double x,y;
Point(double x, double y){ this.x=x; this.y=y; }
public double getX() { return x; }
public void setX(double x) { this.x = x; } }
public class Five {
public static void main(String[] args) { Point p = new Point(1,2); p.setX(3);
System.out.println(p.getX()); } }
6. 创建一个三角形类,有成员变量三条边,有方法求周长,创建主类来测试它。 6. class Triangle{ double a,b,c;
double getLength(){ return a+b+c; }
}
public class Six {
public static void main(String[] args) { Triangle t = new Triangle(); t.a = 2; t.b = 3; t.c = 4;
System.out.println(\周长=\ }
}
7. 编写Java应用程序。首先,定义一个Print类,它有一个方法void output(int x),
1)如果x的值是1,在控制台打印出大写的英文字母; 2)如果x的值是2,在控制台打印出小写的英文字母。
其次,再定义一个主类,在主类的main方法中创建Print类的对象,使用这个对象调
第10页 共13页
Java基础 第3章练习题 大外软件学院
用方法output ()来打印出大小写英文字母。 7. class Print{
void output(int x){ char c; if (x==1){
for(c='A';c<='Z';c++) System.out.print(c); }
if (x==2){
for(c='a';c<='z';c++) System.out.print(c); }
System.out.println(); } }
public class Seven {
public static void main(String[] args) { Print p = new Print(); p.output(1); p.output(2); } }
8. 编写Java应用程序。首先定义一个描述银行账户的Account类,包括成员变量“账号” 和“存款余额”,成员方法有“存款”、“取款”和“余额查询”。其次,编写一个主类,
在主类中测试Account类的功能。 class Account {
String id; double money;
void save(double x) { money = money + x; }
void get(double y) { money = money - y; }
void select() {
System.out.println(\余额=\ } }
public class Eight {
public static void main(String[] args) { Account kaien = new Account(); kaien.save(10000); kaien.get(200); kaien.select();
第11页 共13页
Java基础 第3章练习题 大外软件学院
} }
9. 编写Java应用程序。首先,定义一个时钟类——Clock,它包括三个int型成员变量
分别表示时、分、秒,一个构造方法用于对三个成员变量(时、分、秒)进行初始化, 还有一个成员方法show()用于显示时钟对象的时间。其次,再定义一个主类,在主类 的main方法中创建多个时钟类的对象,使用这些对象调用方法show()来显示时钟的时 间。
9. class Clock{
int hour,second,minute; Clock(int h, int s, int m){ hour = h; minute = m; second = s; }
void show(){
System.out.println(hour + \ }
}
public class Nine {
public static void main(String[] args) { Clock c1 = new Clock(21,16,30); c1.show();
Clock c2 = new Clock(12,12,12); c2.show();
Clock c3 = new Clock(3,3,3); c3.show(); }
}
10.编写Java应用程序。首先,定义描述学生的类——Student,包括学号(int)、姓名
(String)、年龄(int)等属性;二个方法:Student(int stuNo,String name,int age) 用于对象的初始化,outPut()用于输出学生信息。其次,再定义一个主类,在主类的 main方法中创建多个Student类的对象,使用这些对象来测试Student类的功能。 10.class Student{
int id;
String name; int age;
Student(int i, String n, int a){
id = i; name = n; age = a; }
void outPut(){
System.out.println(\学号=\+ id + \姓名=\+ name + \年龄=\+ age); }
第12页 共13页
Java基础 第3章练习题 大外软件学院
}
public class Ten {
public static void main(String[] args) { Student s1 = new Student(1,\ s1.outPut();
Student s2 = new Student(2,\ s2.outPut();
Student s3 = new Student(3,\ s3.outPut(); } }
第13页 共13页
相关推荐: