1、 目录 目录 1 二(1)求球的体积。 3 二(2)求矩形周长和面积。 4 三(1)从键盘输入一整数,根据是奇数还是偶数分别输出odd和even。 5 三(2)输入3个整数,从小到大输出。 5 第五章 7 1.编写一个代表三角形的类,其中,3条边为三角形的属性,并封装有求三角形的面积和周长的方法。分别针对3条边为3,4,5和7,8,9的两个三角形进行测试。 7 5_2 编写一个学生类Student,包含的属性有学号、姓名、年龄,将所有学生 8 五、(3)编写一个Person类,其中包括人的姓名、性别、年龄、子女等属性,并封装有获得姓名、获得年龄、增加1岁、获得子女、设置子女
2、等方法,其中,子女为一个Person数组。 11 5-4.编写一个代表日期的类,其中有代表年月日的三个属性,创建日期对象时要判断参数提供的年月日是否合法,不合法要进行纠正,年默认值为2000;月的值在1到12之间,默认值为1;日由一个对应12个月的整型数组给出合法值,特别的,对于二月,通常为28天,但闰年的二月最多29天,该年的值为400的倍数,或者为4的倍数但不为4的倍数但不为100的倍数的年份为闰年。将创建的日期对象输出时,年月日之间用“/”分隔。 13 5-5 编写一个矩阵类,其中封装有一个代表矩阵的二维数组,并提供一个实现两个相同行、列的矩阵的相加方法。利用随机函数产生两个3行4列
3、的矩阵,验证类设计。 15 五、(6)创建简单的银行账户类。 17 5-7(7)n只猴子要选大王,选举方法如下’:所有猴子按1,2,…,n编号并按照顺序围 19 六(1)给point类添加以下几个求两点间距的多态方法,并进行调用测试。 21 六(2)定义person类,子类student类。 25 六(3)circle类,求圆面积。 28 六(4)复数类,测试两复数的加、乘、求模方法。 30 8(1)定义一个接口,包含一个Display()方法用于显示信息:通知类,汽车类,广告类…… 34 8(2)定义Shape,其中包括一个方法size(),设计矩形、圆、圆柱体等,实现Sha
4、pe接口。 36 8(3)定义一个抽象类水果,包括getweight()方法。 38 8(4)定义Startstop接口,含有start()和stop()两个方法。分别创建会议和汽车两个类实现Startstop接口。 40 (1)classname是否在java类库中。 42 (2) 学生姓名,学号,成绩。 43 1、创建一个名为TestApp的java应用程序,在屏幕上分行显示如下一段文字: 45 华东交通大学 45 欢迎您! 45 2、创建一个名为TestApplet的Applet程序,显示“两个同心圆,园内显示两个汉字‘同心’”,并编写相应的text.html文件。 46
5、 * 47 *** 47 ***** 47 1、球的体积计算公式为:4/3πr^3,编写一个程序输入半径,求体积。 48 2、输入矩形的长和宽,计算矩形的周长和面积。 49 3、从键盘输入摄氏温度C,计算华氏温度F的值并输出,其转换公式如下:F=(9/5)*C+32 49 4、从键盘输入一个实数,获取该实数的整数部分,并求出实数与整数部分的差,将结果分别用两种形式输出:一种是直接输出,另一种是精确到小数点后4为的浮点格式输出。 50 1、从键盘输入一个整数,根据是奇数还是偶数分别输出“odd”和“even”。 51 2、从键盘输入3个整数,按由小到大的顺序排列输出。 51
6、3、从键盘输入a、b、c共3个整数,计算方程ax^2+bx+c=0的根。 53 4、运输公司对用户计算运费,路程越远,折扣越高,标准如下: 54 5、利用下式求e^x的近似值。 56 6、设有一条绳子,长2000米,每天剪去三分之一,计算多少天后长度变为1厘米。 57 7、计算n至少多大时,一下不等式成立: 58 8、编写一个程序,实现从键盘输入10个整数,将最大、最小的整数找出来并输出。 58 9、百鸡百钱问题。公鸡每只3元,母鸡每只5元,小鸡三只1元,用100元钱买100只鸡,公鸡、母鸡、小鸡应各买多少? 59 10、用二重循环输出九九乘法表(注意用制表符“\t”实现结果的对
7、其显示)。 60 11、 输入一个整数,判断该数是否为降序数,是则输出ture,否则输出false。降序数是指该数的各位数字从高到低逐步下降(包括相等)。 61 7_ 1统计字符串中a的出现次数 61 7_2键盘输入若干行文字 62 二(1)求球的体积。 import javax.swing.*; public class V { public static void main(String args[]){ String s=JOptionPane.showInputDialog("请输入球的半径:"); d
8、ouble r=Double.parseDouble(s); double V=Math.PI*r*r*r*3/4; System.out.printf("球的体积=%.3f",V); } } 二(2)求矩形周长和面积。 import javax.swing.*; public class area { public static void main(String args[]){ String x=JOptionPane.showInputDialog("请输入矩形的长:"); String y=JOptionPane.showInpu
9、tDialog("请输入矩形的宽:"); double l=Double.parseDouble(x); double d=Double.parseDouble(y); double c=4*(d+l); double area=d*l; System.out.printf("矩形的周长=%.3f", c); System.out.printf("矩形的面积=%.3f",area); } } 三(1)从键盘输入一整数,根据是奇数还是偶数分别输出odd和even。 import javax.swing.*; public class number
10、 { public static void main(String args[]){ int a; String s; s=JOptionPane.showInputDialog("请输入一个整数:"); a=Integer.parseInt(s); if(a%2==0) System.out.printf("even"); else System.out.printf("odd"); } } 三(2)输入3个整数,从小到大输出。 import javax.swing.*; public class rank { public static void ma
11、in(String args[]){ int a,b,c,t; String x=JOptionPane.showInputDialog("请输第一个整数:"); a=Integer.parseInteger(x); String y=JOptionPane.showInputDialog("请输第二个整数:"); b=Integer.parseInteger(y); String z=JOptionPane.showInputDialog("请输第三个整数:"); c=Integer.parseInteger(z); if(a>b) t=a; a=b; b=t; if
12、b>c) t=b; b=c; c=t; if(a>c) t=a; a=c; c=t; Syatem.out.printf("%d,%d,%d",a,b,c); } } 第五章 1.编写一个代表三角形的类,其中,3条边为三角形的属性,并封装有求三角形的面积和周长的方法。分别针对3条边为3,4,5和7,8,9的两个三角形进行测试。 Public class Triangle{ Private double a,b,c; Public Triangle(double a,double b,double c){ This.a=a; This.b=b; Thi
13、s.c=c;} Public double area(){ Double s=(a+b+c)/2.0; Return Math.sqrt(s*(s-a)*(s-b)*(s-c));} Public double circle(){ Return a+b+c;} Public String toString(){ Return “三角形(”+a+”,”+b+”,”+c+”)”; } Public static void main(String args[]){ Triangle t1=new Triangle(3,4,5); System.out.println(t1+”的
14、面积为:”+t1.area()); System.out.println(t1+”的周长为:”+t1.circle()); Triangle t2=new Triangle(7,8,9); System.out.println(t2+”的面积为:”+t2.area()); System.out.println(t2+”的周长为:”+t2.circle()); } } 5_2 编写一个学生类Student,包含的属性有学号、姓名、年龄,将所有学生 存储在一个数组中,自拟数据,用数组的初始化方法给数组赋值,并实现如下操作 1>将所有学生的年龄增加1岁; 2>按数组中顺序显示
15、所有学生的信息。 3>查找显示所有年龄大于20岁的学生的名单。 */ public class Student { int num; int age; String name; public String toString() { return"学号:"+num+",姓名:"+name+",年龄:"+age; } public Student(int Num,int Age,String Name) { num=Num; age=Age; name=Name; } public static void main(Strin
16、g args[]) { Student s1=new Student(3,18,"张三"); Student s2=new Student(1,21,"小路"); Student s3=new Student(33,20,"John"); Student s4=new Student(13,20,"Lucy"); Student s5=new Student(8,17,"Jack"); Student s[]={s1,s2,s3,s4,s5}; System.out.println("班级学生名单如下:"); output(s); fo
17、r(int i=0;i 18、System.out.println(s[i]);
}
}
五、(3)编写一个Person类,其中包括人的姓名、性别、年龄、子女等属性,并封装有获得姓名、获得年龄、增加1岁、获得子女、设置子女等方法,其中,子女为一个Person数组。
用某实际数据测试该类的设计。
public class Person{
private String name;
private int age;
private Person[]children=null;
public Person(String myname,int myage){
name=myn 19、ame;
age=myage;
}
public Person(String name1,int age1,Person chs[]){
name=name1;
age=age1;
children=chs;
}
public String toString(){
String s="Name:"+ name+"/n";
s+="Age"+age+"/n";
return s;
}
public String getName(){
return name;
}
public String 20、getAge(){
return age;
}
public void incAge(){
age++;
}
public Person[] getChidren(){
return children;
}
public void setChildren(Person[] mychildren){
children=mychildren;
}
public static void main(String args[]){
Person p1=new Person("John",20);
Person p2= 21、new Person("Marry",18);
Person[]c1={p1,p2};
Person p3=new Person("Smith",50,c1);
System.out.println("father:"+p3+"have following children");
Person[]mychild=p3.getChildren();
for (int k=0;k 22、
}
5-4.编写一个代表日期的类,其中有代表年月日的三个属性,创建日期对象时要判断参数提供的年月日是否合法,不合法要进行纠正,年默认值为2000;月的值在1到12之间,默认值为1;日由一个对应12个月的整型数组给出合法值,特别的,对于二月,通常为28天,但闰年的二月最多29天,该年的值为400的倍数,或者为4的倍数但不为4的倍数但不为100的倍数的年份为闰年。将创建的日期对象输出时,年月日之间用“/”分隔。
Public class Date{
Private int year=2000;
Private int month=1;
Private int day;
23、Public Date (int theYear,int theMonth,int theDay){
If(theMonth>0&&theMonth<=12)
month=theMonth;
else
month=1;
year=theYear;
day=checkDay(theDay);
}
Private int checkDay(int testDay){
Int daysPerMonth[]={31,28,31,30,31,30,31,31,30,31,30,31};
If(month==2&&testDay<=29&&(year%400==0||(year%4== 24、0&&year%100!=0)))
Return testDay;
If(testDay>0&&testDay<=daysPerMonth[month-1])
Return year+”/”+month+”/”+day;
}
Public String toString(){
Return year+”/”+month+”/”+day;}
Public static void main(String args[]){
Date d=new Date(2010,3,29);
System.out.println(“日期为:”+d);
Date m=new Date(2010, 25、2,30);
System.out.println(“日期为:”+m);
}
}
5-5 编写一个矩阵类,其中封装有一个代表矩阵的二维数组,并提供一个实现两个相同行、列的矩阵的相加方法。利用随机函数产生两个3行4列的矩阵,验证类设计。
public class Matrix{
int m;
int n;
int [][] num;
public Matrix(int m,int n){
this.m=m;
this.n=n;
num=new int[m][n];
}
public void initialM 26、atrix(){
for(int i=0;i 27、j 28、rix(3,4);
Matrix m2=new Matrix(3,4);
m1.initialMatrix();
m2.initialMatrix();
Matrix m3=plus(m1,m2);
m1.output();
System.out.println("--------------");
m2.output();
System.out.println("--------------");
m3.output();
}
}
五、(6)创建简单的银行账户类。
public class BankUser{
pr 29、ivate String accout;
private String name;
privat double money;
public Bankuser(String accout,String name,double money){
this.account=account;
this.name=name;
this.money=money;
}
public double query(){
return money;
}
public void save(dou 30、ble x){
money +=x;
}
public double draw(double x){
if(money>x){
money-=x;
return money;
}else{
return -1;
}
public static void main(String[] args){
BankUser user1=new BankUser("61203329","mary",500);
Bank 31、User user2=new BankUser("61204356","张三",1500);
System.out.println("用户:"+user1.account+"余额:"+user1.query());
user1.save(100);
user1.draw(45);
user1.draw(500);
System.out.println("用户:"+user1.account+"余额:"+user1.query());
Systom.out.println("用户:"+us 32、er2.account+"余额:"+user2.query());
}
}
5-7(7)n只猴子要选大王,选举方法如下’:所有猴子按1,2,…,n编号并按照顺序围
成一圈,从第k个猴子起,由l开始报数,报到m时,该猴子就跳出圈外,下一只猴子再
次由l开始报数,如此循环,直到圈内剩下一只猴子时,这只猴子就是大王。
1. 输入数据:猴子总数n,起始报数的猴子编号k,出局数字为m
2. 输出数据:猴子的出队序列和猴子大王的编号。
public class Monkey{
int number;
public Monkey(int num)‘
33、 number=num;
)
public static void main(String[】args){
int n=32;
int m=3;
Monkey mon[1=new Monkey[n];
for (int k=O;k 34、if(p==m){
System.out.print(mon[j].number+” ”):
mon[j] = null;
p=0:
k++;
}
j_(j+1)%n;
}
System.out.p~intln():
for(int i=0;i 35、class Point{
int x;
int y;
public Point(int x,int y){
this.x=x;
this.y=y;
}
public double distance(Point p){
return Math.sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
}
public double distance(int x,int y){
return Math.sqrt((this.x-x)*(this.x-x)+(this.y-y)*(th 36、is.y-y));
}
public static double distance(Point x,Point y){
return Math.sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
}
}
public class hc {
public static void main(String args[]){
Point p=new Point(3,9);
Point a=new Point(7,6);
System.out.println(p.distan 37、ce(a));
System.out.println(p.distance(5,8));
System.out.println(Point.distance(p,a));
}
class Person{
String name;
char sex;
int age;
public Person(String name,char sex,int age){
this.name=name;
this.sex=sex;
this.age=age;
}
}
class teac 38、her extends Person{
String pro;
String partement;
public teacher(String name,char sex,int age,String pro,String partement){
super(name,sex,age);
this.pro=pro;
this.partement=partement;
}
public String toString(){
return name+","+sex+","+age+","+pro+","+parteme 39、nt;
}
}
六(2)定义person类,子类student类。
class student extends Person{
String num;
String time;
String zhuanye;
public student(String name,char sex,int age,String num,String time,String zhuanye){
super(name,sex,age);
this.num=num;
this.time=time;
this.zhuan 40、ye=zhuanye;
}
public String toString(){
return name+","+sex+","+age+","+num+","+time+","+zhuanye;
}
}
public class hc {
public static void main(String args[]){
student a=new student("张三","boy","22","0913","2011/9","网络工程");
System.out.println("学生基本信息:"+a);
te 41、acher b=new teacher("李四","boy","40","教师","数信学院");
System.out.println("教师信息:"+b);
}
}
六(3)circle类,求圆面积。
class Circle{
double r;
public Circle(double r){
this.r=r;
}
public double area(){
return Math.PI*r*r;
}
public static doub 42、le area(double r){
return Math.PI*r*r;
}
public static double area(Circle c){
return Math.PI*c.r*c.r;
}
}
public class hc {
public static void main(String args[]){
Circle c1=new Circle(3);
System.out.println(c1.area());
System.out.println(Circle.area(3)); 43、
System.out.println(Circle.area(c1));
}
}
六(4)复数类,测试两复数的加、乘、求模方法。
public class Hc {
private double x,y;
public Hc(double a,double b){
x=a;
y=b;
}
public String toString (){
return "("+x+","+y+"i"+")";
}
public Hc add(Hc a){
return new 44、 Hc(x+a.x,y+a.y);
}
public Hc add(double a,double b){
return new Hc(x+a,y+b);
}
public Hc multiply(Hc a){
return new Hc(x*a.x,y*a.y);
}
public Hc multiply(double a,double b){
return new Hc(x*a,y*b);
}
public double getvalue(){
return Math.sqrt(x* 45、x+y*y);
}
public static void main(String args[]){
Hc x,y,z;
x=new Hc(1,2);
y=new Hc(2,4);
z=x.add(y);
System.out.println("return="+z);
z=x.add(4,5);
System.out.println("return="+z);
z=x.multiply(y);
System.out.println("return="+z);
z=x.mu 46、ltiply(5,6) ;
System.out.println("return="+z);
double m=x.getvalue();
System.out.println(x+"的模="+m);
}
}
8(1)定义一个接口,包含一个Display()方法用于显示信息:通知类,汽车类,广告类……
interface Display{
void display();
}
class Inform implements Display{
public void display(){
System.out.println("通知 47、内容");
}
}
class Car implements Display{
public void display(){
System.out.println("汽车油亮");
}
}
class Adervise implements Display{
public void display(){
System.out.println("广告消息");
}
}
public class Java1{
public static void main(String[]args){
Display []s={new Inform(),new 48、 Car(),new Adervise()};
for (int i=0;i 49、1=x1;this.y1=y1;
this.x2=x2;this.y2=y2;
}
public double size(){
return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
}
class Circle implements Shape{
private double r;
public Circle(double r){
this.r=r;
}
public double size(){
return 3.14*r*r;
}
}
class Conference imp 50、lements Shape{
private double peoples;
public Conference(double p){
peoples=p;
}
public double size(){
return peoples;
}
}
class 圆柱体 implements Shape{
private Circle c;
private double h;
public 圆柱体(Circle c1,double h1){
c=c1;h=h1;
}
public double size(){
return c.si






