JAVA课堂例题(2)

2019-02-15 18:43

数:Student()

Student(String name,int age) Student(String name,int age,int x,int y,int z)在上面的Student

类里,定义一个printStudentInformation()方法,显示学生的信息。

①创建学生对象student1后,给他的所有属性赋值,然后调用方法输出. ②创建姓名为“李乐”,年龄为15的学生对象student2,给他的各科成绩赋值,然后调用方法输出.

③创建姓名为“张天”,年龄12,成绩分别为98,95,96的学生对象student3,然后调用方法输出.

public class Student {

String name; int age;

int math,chinese,english; public Student() { }

public Student (String name,int age) {

this.name=name; this.age=age; }

public Student(String name,int age,int x,int y,int z) {

this(name,age); math=x; chinese=y; english=z; }

public void printStudentInformation() {

System.out.println(\姓名\年龄\数学成绩\语文成绩\英语成

绩\}

public static void main(String[] args) {

Student student1=new Student(); student1.printStudentInformation(); student1.name=\李伟\student1.age=20; student1.math=90;

student1.chinese=80; student1.english=99;

student1.printStudentInformation();

Student student2=new Student(\李乐\student2.printStudentInformation(); student2.math=80; student2.chinese=100; student2.english=60;

student2.printStudentInformation();

Student student3=new Student(\张天\student3.printStudentInformation(); } }

------------------------------------------------------------------------------------------

定义一个描述长方体的类Box,类中有三个整型的成员变量:length、width和height,分别表示长方体的 长、宽和高。定义构造函数,初始化这三个变量;定义方法求长方体的体积并返回整型结果;定义方法求

长方体的表面积整型结果;定义方法把长方体的长、宽和高以及长方体的体积和表面积转化为字符串并返

回字符串。编写应用程序,测试类Box,使用类中定义的各个方法,并将其结果输出。

public class Box {

int length; int width; int height;

public Box (int length,int width,int height ) {

this.length = length; this.width = width; this.height = height; }

public int Tiji( ) {

return length*width*height;

}

public int Biaomianji( ) {

return 2*(length*width+length*height+width*height); }

public String toString( ) {

return \长方体的长 = \宽= \高=\体积=\

表面积= \ }

public static void main(String args[]) {

Box box=new Box(2,3,4);

System.out.println(box.toString()); } }

------------------------------------------------------------------------------------------

编写应用程序Volume,程序中共有3个重载方法calVolume,每个方法的参数不同,分别计算长方体、球体和

圆柱体的体积

------------------------------------------------------------------------------------------

定义Point类,其有属性有点x,y,构造方法public Point(int p1,int p2),方法有public int getX

(),public void setX(int x),public int getY(),public void setY(int Y) 定义应用程序TestPoint(),创建(4,5)(7,8)两个点对象,并求出这两点间的距离输出

public class Point {

private int x,y;

public Point(int p1,int p2); { x=p1; y=p2; }

public void setX(int x) {

this.x=x; }

public int getX() {

return this.x; }

public void setY(int y) {

this.y=y; }

public int getY() {

return this.y; } }

public class TestPublic {

public static void main(String[] args) {

Point p1=new Point(4,5); Point p2=new Point(7,8); double l=0;

l=Math.squrt(Math.paw(p1.getX()-p2.getX()),2)+Math.squrt(Math.paw(p1.getY()-p2.getY()),2) System.out.println(\点(\和点(\之间的距

离为:\}

} (尚需探讨)

------------------------------------------------------------------------------------------ 4.6 上机实践 P90

public class A {

private int data; private String str; public A() {

data=6;

str=\ \}

public A(int data, String str)

{

this.data=data; this.str=str; }

public void add(int k, String s) {

data+=k(date=date+k); str+=s(str=str+s); }

public String toString() {

return data +\ \}

public void clearA() {

data = 6;

str = \ \} }

public class TestA{

public static void main(String[] args) { A s=new A(); s.add(9,\

System.out.println(s.toString()); s.clearA(); } }

------------------------------------------------------------------------------------------

①定义Person类,Student类和TestStudent类,其中Person类中有protected的变量name,sex,age,另外有无

参方法体为空的构造方法,有带三个输入参数给三个成员变量赋初始值的构造方法,还有一个返回值为

String类型的toString()方法用于返回个人的详细信息。

②Student类的直接父类Person类,他也有两个可重载的构造方法,Student类除了继承父类的属性和方法外

,还增加了数学、语文、英语的成绩以及求平均分的方法,另外在Student类中重写父类的toString()方法

,除了返回个人的基本信息外,还需返回学生的平均成绩。


JAVA课堂例题(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:家庭实验:自制汽水

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: