JAVA课堂例题(3)

2019-02-15 18:43

③在TestStudent类中创建两个学生对象,输出学生的详细信息以及平均成绩。

public class Person {

protected String name; protected String sex; protected int age; pblic Person() { }

public Person(String name,String sex,int age) {

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

public String toString() {

String s=\姓名:\性别:\年龄:\return s; } }

public class Student extends Person {

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

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

this.chinese=chinese; this.math=math;

this.english=english; }

public double average() {

double ave=(chinese+math+english)/3; return ave; }

public String toString {

String s=\姓名:\性别:\年龄:\平均分为:\return s; } }

public class TestStudent {

public static void main(String args[]) {

Student s1=new Student(95,85,90); s1.name=\小宁\s1.age=21; s1.sex=\女\

System.out.println(s1.toString); Student s2=new Student(99,100,98); s1.name=\小林\s1.age=21; s1.sex=\男\

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

------------------------------------------------------------------------------------------ 请按如下要求定义两个A和B:

①类A中定义一个int类型变量z(将其赋值为16)、一个char类型变量x(将其赋值为65)和一个方法

myPrint(),该方法的功能是在屏幕上输出z的值和x的值。

②类B是类A的子类,其中定义了一个double类型的变量y(将其赋值为16.0)和String类型的对象s(将其

赋值为java program!),还定义了两个方法myPrint()和printAll(),方法myPrint()的功能是在屏幕上

输出y和s的值,方法printAll()的功能是分别调用父类和子类的myPrint()方法。

③编写应用程序,创建B类的对象b,调用printAll()方法用来显示z、x、y、s的值。

/*类A中定义一个int类型变量z(将其赋值为16). 一个char类型变量x(将其赋值为65)

和一个方法myprint().该方法的功能是在屏幕上输出z和x的值*/ class A

{

int z =16; char x = 65;

public void myprint() {

System.out.println(z); System.out.println(x); } }

/*类B是类A的子类.其中定义了一个double类型的变量y * (将其赋值为16.0)和String类型的对象s

(将其赋值为java program!).还定义了两个方法myprint()和printAll().

方法myprint()的功能是在屏幕上输出y和s的值.方法printAll()的功能是分别调用父类和子类的方法

myprint(). */

class B extends A {

double y = 16.0;

String s =\public void myprint() {

System.out.println(y); System.out.println(s); }

public void printAll() {

super.myprint(); myprint(); } } /*

* 写一个Application应用程序.创建类B的对象b.调用printall()方法用来显示z.x.y.s的值. * */

public class C {

public static void main(String[] args) {

B b = new B(); b.printAll(); }

}

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

①定义一个Rectangle类,有属性长和宽,有一个默认形式的构造函数,一个用两个形参给成员赋值的构造

函数,另外有求长方形面积的方法CalArea() ②Circle类是Rectangle类的子类,在Circle类中重新定义边长属性和求面积的方法CalArea() ③定义测试类,创建Rectangle类对象r1,并输出对象的属性值及面积值,创建Circle类对象s1输出及属性

值及面积值,创建Circle类对象赋给Rectangle类对象r2,并输出及属性值及面积值

class Rectangle {

double length; double width; public Rectangle() { }

public Rectangle(double length,double width) {

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

public double CalArea() {

return length*width; } }

class Cricle extends Rectangle {

double length,width; public Cricle() { }

public Cricle(double r) {

length=r; width=r; }

public double CalArea() {

return (Math.PI*(length/2)*(length/2)); } }

public class TestCricle {

public static void main(String[] args) {

Rectangle r1=new Rectangle(20,15);

System.out.println(\长为:\宽为:\面积为:\Cricle st=new Cricle(16);

System.out.println(\直径为:\面积为:\Rectangle r2=new Cricle(21);

System.out.println(\长为:\宽为:\面积为:\} }

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 5.12上机实践 P124

------------------------------------------------------------------------------------------ 作业一 interface A {

long factorial(int m);

long myPower(int m,int n);

boolean findFactorSum(int m,int n); }

class A1 implements A {

private int i=0,sum=1; public A1 () { }

public long factorial(int m) {

for(i=1;i

sum=sum*i; }

return (sum); }


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

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

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

马上注册会员

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