9.26java实验题2

更新时间:2023-12-30 12:45:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

Chapter 3问答

1、 main方法的访问权限为何必须为public、static?网上搜

2、 面向对象的语言有哪三个特性?封装,继承,多态(重写,重载。区别是什么) 请用自己的理解描述一下三个特性。

3、 面向对象的软件开发有哪些优点 1、

引用传递是将参数的引用传递给被调用方法,被调方法通过传递的引用值获取其指向的内存空间,从而在原始内存空间直接进行操作。根据下列代码,给出程序的完整输出结果。 public class CallByRef { int a, b; CallByRef(int i, int j) { a = i; b = j; } void change(CallByRefobj) { obj.a = 50; obj.b = 40; System.out.println(\在change方法中obj.a=\ } public static void main(String[] args) { CallByRefobj = new CallByRef(15, 20); System.out.println(\调用change方法前obj.a=\ obj.change(obj); System.out.println(\调用change方法后obj.a=\ } }

输出结果为:

调用change方法前obj.a=15,obj.b=20 在change方法中obj.a=50,obj.b=40 调用change方法后obj.a=50,obj.b=40

2、按值传递将要传递的参数的值传递给被调方法,被调方法通过创建一份新的内存拷贝来存储传递的值,然后在内存拷贝上进行数值操作,所以按值传递不会改变原始参数的值。根据下列代码,给出程序的完整输出结果。

public class CallByValue { public static void main(String[] args) { intnum = 5; System.out.println(\调用change方法前 : \ CallByValuecallByValue = new CallByValue(); callByValue.change(num); System.out.println(\调用change方法后 : \ } public void change(intnum) { num += 5; System.out.println(\方法中num的值为:\ } }

输出结果为:

调用change方法前:5

change方法中num的值为:10 调用change方法后 :5

3、课后8 编写一个程序,计算箱子的体积,要求将箱子的高度、宽度和长度参数的值传递给构造方法,计算并显示体积。

class Box{ private double h; private double w; private double l; public Box(double h,doublew,double l){ this.h=h; this.w=w; this.l=l; } public double volume(){ return h*w*l; } }

public class Test1 { public static void main(String[] args) { Box b=new Box(10,10,10); System.out.println(\体积为:\ } }

4、课后9 编写Point类,有两个属性x、y,一个方法distance(Point p1,Point p2),计算两者之间的距离。

public class Point { private double x; private double y; public Point(double x,double y){ this.x=x; this.y=y; } public double distance(Point p1,Point p2){ double r=Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); return r; } public static void main(String[] args) { Point p1=new Point(1,4); Point p2=new Point(5,1); System.out.print(\两Point之间的距离为\ } }

5、编写一个圆类Circle,并创建对象测试该类。Circle类拥有:

1)个成员变量

r(私有,浮点型); // 存放圆的半径; 2)两个构造方法

Circle( ) // 将半径设为0

Circle(double r ) //创建Circle对象时将半径初始化为r 3)三个成员方法

double getArea( ) //获取圆的面积 double getPerimeter( ) //获取圆的周长

void show( ) //将圆的半径、周长、面积输出到屏幕

class Circle{

final double PI=3.14; //注意与Math.PI的区别 private double r; circle(){ radius=0.0; } circle(double r){ radius=r; } public double getPerimeter(){ return 2*PI*r;

} public double getArea(){ returnPI*r*r; } void show(){ System.out.println(\圆的半径:\ System.out.println(\圆的周长:\ System.out.println(\圆的面积:\ } }

public class Test { public static void main(String[] args) { Circle c=new Circle(10); c.show(); } }

Chapter 4

1.什么叫多态?如何理解多态。

1、 简述overload和override的区别。

2、 是否可以从一个static方法内部发出对非static方法的调用?

1.定义一个表示学生信息的类Student,要求如下:

定义一个满足下列条件的学生类,并创建其对象,进行测试。

此学生类拥有属性:名字name、学号num、分数(共5门课程)score[] 要求编写学生类,并实现如下要求:

(1)利用Scanner类,编写一个接受分数输入的方法setscore(); (2)编写一个计算总分的方法gettotal();

(3)编写一个计算平均分的方法getaverage(); (4)以姓名\王文\学号 2010222111,实例化该学生类,并在控制台输出该学生的姓名,学号,以及总分和平均分。

class Student { String name; intnum; double score[] = new double[5]; double total = 0; double average; Student(String name, intnum) { this.name = name; this.num = num; for (int i = 0; i <5; i++) score[i] = 0; } voidsetscore() { System.out.println(\请输入五门成绩:\ for (int i = 0; i < 5; i++) { Scanner input = new Scanner(System.in); score[i] = input.nextInt(); } } doublegettotal() { for (int i = 0; i < 5; i++) total += score[i]; return total; } doublegetaverage() { average = total / 5; return average; } void display() { System.out.println(\学号为:\ System.out.println(\姓名为:\ System.out.println(\总分为\ System.out.println(\平均成绩为\ } }

public class S_student1 { public static void main(String args[]) { Student stu = new Student(\王文\ stu.setscore(); stu.display(); } }

2、定义一个表示学生信息的类

Student,要求如下:

(1)类Student的成员变量:

sNO表示学号;sName表示姓名;sSex表示性别;sAge表示年龄;sJava:表示Java课程成绩。

(2)类Student带参数的构造方法:

在构造方法中通过形参完成对成员变量的赋值操作。 (3)类Student的方法成员: getNo():获得学号; getName():获得姓名; getSex():获得性别; getAge()获得年龄;

getJava():获得Java 课程成绩

(4)根据类Student的定义,创建五个该类的对象(自己定义即可),输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。

public class Student { String sNO,sName,sSex; intsAge,sJava; public Student(String XH,StringXM,StringXB,intNL,int XF) { super(); sNO=XH; sName=XM; sSex=XB; sAge=NL; sJava=XF; } public String getNO() { returnsNO; } public String getName() { returnsName; } public String getSex() { returnsSex; } publicintgetAge() { returnsAge; } publicintgetJava() { returnsJava; } public static void main(String[] args) { Student[] st=new Student[5];

st[0]=new Student(\张三\男\ st[1]=new Student(\李四\男\ st[2]=new Student(\王五\女\ st[3]=new Student(\赵六\男\ st[4]=new Student(\杨七\女\ int max=0,min=100,sum=0; System.out.println(\学生信息:\ for (int i=0;i max) max=st[i].sJava; sum=sum+st[i].sJava; System.out.println(\学生编号:\,姓名:\,性别:\,年龄:\,Java课学分:\

} System.out.println(); System.out.println(\共有学生:\,平均成绩:\ System.out.println(\最小学分:\,最大学分:\ } } 3、

编程输入学生的学习成绩的等级,给出相应的成绩范围。设A级为85分以上(包括85分);B级为70分以上(包括70分);C级为60分以上(包括60分);D级为60分以下。分别使用if语句和switch语句实现。

IF语句

public class dengji { public static void main(String[] args) { System.out.println(\请输入等级:\ Scanner sc=new Scanner(System.in); String grade=sc.next(); if(grade.equals(\

System.out.println(\范围在[85,100)\else if(grade.equals(\ System.out.println(\范围在[70,85)\else if(grade.equals(\ System.out.println(\范围在[60,70)\else if(grade.equals(\

System.out.println(\范围在[0,60)\else

System.out.println(\输入有误!\ } }

Switch 语句

publicclass Student { publicstaticvoid main(String[] args){ char grade=' '; System.out.println(\请输入等级:\); Scanner sc=newScanner(System.in); String s=sc.next(); grade=s.charAt(0); switch(grade){ case'A': System.out.println(\范围在85~100\); break; case'B': System.out.println(\范围在70~85\); break; case'C': System.out.println(\范围在60~70\); break; case'D': System.out.println(\范围在60分以下\); break; default: System.out.println(\输入有误!\); } } } 4、

编程实现动物世界的继承关系。动物(Animal)具有行为:吃(eat)、睡觉(sleep); 动物包括:兔子(rabbit)、老虎(tiger);这些动物吃的行为各不相同(兔子吃草、老虎吃肉),但睡觉的行为是一致的。请通过继承实现以上需求,并编写测试类AnimalTest进行测试。

class Animal{ void eat(){ System.out.println(\ System.out.println(\ } void sleep(){ System.out.println(\ } }

class Rabbit extends Animal{ void eat() { super.eat(); System.out.println(\ System.out.println(\

} }

class Tiger extends Animal{ void eat() { super.eat(); System.out.println(\ System.out.println(\ } }

public class AnimalTest { public static void main(String[] args) { Animal animal=new Animal(); animal.eat(); Rabbit rabbit=new Rabbit(); rabbit.eat(); rabbit.sleep(); Tiger tiger=new Tiger(); tiger.eat(); tiger.sleep(); } }

6、编写程序,测试字符串“你好,欢迎来到Java世界”的长度,将字符串的长度转换成字符串进行输出,并对其中的“Java”四个字母进行截取,输出截取字母以及它在字符串中的位置。 }

public class jiequ {

publicstaticvoid main(String[] args) { String a=new String(\你好,欢迎来到Java世界\); int i=a.length(); int j; char[] c=a.toCharArray(); System.out.println(String.valueOf(i)); for(j=0;j

7、创建一个职工类(Employee),公司的职工分为销售员(CommissionWorker)、计件工(PieceworkWorker)和小时工(HourlyWorker)。销售员的收入是基本工资加上销售额的5%;计件工类的收入取决他生产的工件数量;小时工的收入以小时计算,若超过40小时部分的报酬是平时的1.5倍。在测试类中为每一个类建立一个实例,放于数组中,依次打印其工资。

public class EmployeeTest { public static void main(String[] args) { finalint BASE = 1000; Employee[] emps = new Employee[3]; emps[0]=newCommissionWorker(BASE,\ emps[1] = new PieceworkWorker(BASE, \ emps[2] = new HourlyWorker(BASE, \ for (int i = 0; i

abstract class Employee { int base; final String NAME; publicintgetBase() { return base; } public void setBase(int base) { this.base = base; } public Employee(int base, String name) { super(); this.base = base; NAME = name; } public Employee(String name) { this(1000, name); } public String getName() { return this.NAME; } abstract double getSalary(); public String toString() { return \的工资是\ } }

classCommissionWorker extends Employee {

privateint sales; publicCommissionWorker(int base, String name, int sales) { super(base, name); this.sales = sales; } public double getSalary() { return base + sales * 0.05; } }

classPieceworkWorker extends Employee { privateint pieces; publicPieceworkWorker(int base, String name, int pieces) { super(base, name); this.pieces = pieces; } public double getSalary() { return base + pieces * 3; } }

classHourlyWorker extends Employee { privateint hours; publicHourlyWorker(int base, String name, int hours) { super(base, name); this.hours = hours; } doublegetSalary() { double extra = hours > 40 ? hours * 6 * 1.5 : hours * 6; return base + extra; } }

8、

为某研究所编写一个通用程序,用来计算每一种交通工具运行1000公里所需的时间,

已知每种交通工具的参数都是3个整数A、B、C的表达式。现有两种工具:Car和Plane,其中Car的速度运算公式为:A*B/C,Plane 的速度运算公式为:A+B+C。

要求在未来如果增加第3种交通工具的时候,不必修改以前的任何程序,只需要编写新的交通工具的程序。

class Car implements Common{ public double speed(intA,intB,int C){ double D=A*B/C; return D; } }

class Plant implements Common{ public double speed(intA,intB,int C){ double D=A+B+C; return D; } }

class A implements Common{ public double speed(intA,intB,int C){ double D=A+B+C; return D; } }

public class Test2 { public static void main(String[] args){ Car c=new Car(); Plant p=new Plant(); int A=3,B=5,C=2; double t1=1000/c.speed(A, B, C); System.out.println(\汽车需要的时间\ System.out.print(\飞机需要的时间\ } }

本文来源:https://www.bwwdw.com/article/ak1x.html

Top