JAVA复习题(8)

2019-02-16 14:31

36

f.smethod();

答案: 正确

System.out.println(Foo.i);

答案: 错误

System.out.println(Foo.s);

答案: 正确

Foo.imethod();

答案: 错误

Foo.smethod();

答案: 正确

20. 根据下面的程序,指出下面每个元素的作用域(类作用域或块作用域)变量x 变量y 方法cude 变量 i 变量 yPos

public class CubeTest { int x; public void print() { int yPos = 10; for(x=1;x<=10;x++) { System.out.println(cude(x)); for(int i=1;i<=yPos;i++) System.out.println(\ } }

public int cude (int y) { return y*y*y; } } 答案:

变量x : 类作用域 变量y : 块作用域 方法cude : 类作用域

36

37

变量 I : 块作用域 变量 yPos : 块作用域

五、编程题

1、基本类定义:

编写一个类,描述学生的学号、姓名、成绩。学号用整型,成绩用浮点型,姓名用String类型。编写一个测试类,输入学生的学号和成绩,并显示该学号的学生姓名,以及成绩。 class Student{ }

public class T1_Student{

public static void main(String[] args){

Student s=new Student(); s.sNum=101; s.score=97; s.name=\张三\

System.out.println(s.name+\的学号是\成绩是\} int sNum; double score; String name;

}

2.(TestEmployee.java)定义并测试一个代表员工的Employee类,它的属性包括“员工姓名”、“员工号码”、“员工基本薪水”、“员工薪水增长额”;它的方法包括“构造方法”、“获取员工姓名”、“获取员工号码”、“获取员工基本薪水”、“计算薪水增长额”及“计算增长后的工资总额”。 class Employee {

private String name; private long id; private double salary; private double increase;

Employee(String name,long id,double salary) { }

String getname() { }

long getid() {

37

return name; this.name=name; this.id=id; this.salary=salary;

38 }

public class TestEmployee { }

public static void main(String[] args) { } }

double getsalary() { }

double raise(double percent) { }

double total() { }

return (increase+salary); increase=percent*salary; return(increase); return salary; return id;

Employee Jack=new Employee(\System.out.println(\姓名:\System.out.println(\

System.out.println(\基本薪水:\System.out.println(\增加新水:\System.out.println(\新水总额:\

3.写一个名为Account的类模拟账户。该类包含账户、余额、年利率等属性,包含提款、存款方法和访问器方法,类结构如下表。

写一个用户程序测试Account类。在用户程序中,创建一个账号为1122、余额为20000、年利率为4.5%的Account对象。使用withdraw方法提款2500,使用deposit方法存款3000,并打印余额。

Account private int id private double balance private double annualInterestRate public Account() public Account(int id, double balance , double annualInterestRate) public int getId() public double getBalance() public double getAnnualInterestRate () public void setId(int id) public void setBalance(double balance) public void setAnnualInterestRate (double annualInterestRate) 38

39

public void withdraw(double amount) public void deposit(double amount)

public class Test {

public static void main(String[] args) {

Account account = new Account (1122, 20000,0.045);

account.withdraw(2500);

System.out.println(\

account.deposit(3000);

System.out.println(\

} }

class Account {

private int id

private double balance

private double annualInterestRate

public Account () { id=0; balance = 0; annualInterestRate = 0;

}

public Account(int id, double balance , double annualInterestRate) { this.id = id; this.balance = balance; this. annualInterestRate = annualInterestRate; }

public String getId() {

return id; }

public String getBalance () {

return balance; }

public double getAnnualInterestRate () {

return annualInterestRate; }

public void setId(int id) {

this.id = id; }

public void setBalance(double balance) {

this.balance = balance; }

public void setAnnualInterestRate (double annualInterestRate) {

this. annualInterestRate = annualInterestRate; }

39

40

public void withdraw(double amount) {

balance -= amount;

}

public void deposit(double amount) {

balance +=amount;

} }

4.定义一个类Rectangle代表矩形,其中包括计算面积的方法。再定义一个它的子类Square代表正方形],其中也包含计算面积的方法。编写一程序,测试新建子类对象的属性和方法。

class Rectangle{ }

class Square extends Rectangle{ }

public class TestRectangle{ }

40

public static void main(String[] args){ }

Square sq=new Square(5.2f);

System.out.println(\float length;

Square(float len){ }

float getArea(){ }

return super.getArea(); super(len,len); length=len; float length; float width;

Rectangle(float len,float wh){ }

float getArea(){ }

return length*width; length=len; width=wh;


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

下一篇:电容式传感器的位移特性实验

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

马上注册会员

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