77范文网 - 专业文章范例文档资料分享平台

JAVA复习题(8)

来源:网络收集 时间:2019-02-16 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

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;

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库JAVA复习题(8)在线全文阅读。

JAVA复习题(8).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/477165.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: