XTU-JAVA课后练习答案二

《面向对象程序设计(Java)》
实验报告

实验2: 类的设计和对象的创建、成员变量和成员方法的设计

一、实验类型
设计性实验
二、实验目的
1、掌握各种流程控制语句
2、熟练应用数组的定义和使用
3、OOP实例的调试与运行

三、实验内容及步骤
一、流程控制语句
Java程序的流程控制语句用于控制程序中各个语句的执行顺序。
包括:选择语句、循环语句、跳转语句
1、选择语句包括:if 语句和swith语句
实例1:给出3个整型数,找出最大的数。
public class Ul{
public static void main(String argsJ){
int max,a=4,b=3,c=7;
/具体程序内容由学生完成
System.out.println(a+““+b+”“+c);/如果改为(a+b+e)结果如何?
System.out.println(“max=”+max);实例2:根据变量score中存放的考试分数,输出对应的等级。
60分以下为D等;6069为C等;7089为B等;90~100为A等。
public class U2{
public static void main(String argsJ){
int score=55;switch(score/10){
case 0:case 1:case 2:case 3:case 4:case 5:System.out.println(score+"分是D等”);break;
//去掉break 结果有何变化?
case 6:System.out.println(score+“分是C等”);break;case 7:
case 8:System.out.println(score+“分是B等”);break;case 9:
case 10:System.out.println(score+“分是A等”);break;default:System.outprintln(“数据错误”;
}}}
2、循环语句
实例3:计算1+2+…+100的结果。
程序自己编写
3、计算并输出n的阶乘(设n=10)。
程序自己编写
4、编写程序,输出1~l00间的所有奇数。
提示:满足表达式(i9%2l-0)的i值为奇数
二、数组的定义和使用
实例:4,二维数组的定义和使用
public class Shuzul(
public static void main(String args[])throws Java.io.IOException{
char ch[][]=new char[4][4];System.out.println(“输入16个字母,输入b跳出”;labl:
for(int i=0;i<4;i++)for(int j=0:j<4j++){
ch[i]i]=(char)System.in.readO;System.in.skip(2);
/输入一个字母回车一次,回车不会被认做字母赋给数组
if(chf[i][j]==b)
break labl;/∥去掉该处和第5行的labl,结果又会如何?
}
for(inti=0;i<4;i++)/∥用于输出数组ch for(intj=0:j<4j++)
System.out.print(ch[i][i]+””);/"M“用于分隔每个数组元素System.out.println("跳出”;思考:去掉第5行和第12行的labl,结果又会如何?为什么会这样?
三、OOP实例的调试与运行

(2) 题目从上机练习系统中选取
(3) 每道题都需要“类描述图”
(4) 每道题都需要实验过程和结果的屏幕截图
第1题(上机练习系统第46题 )
第46题 填写代码. Point类例子(1)(10分)
下面程序是Point类的基本操作例子.
请将下面程序的【代码】替换为Java程序代码,使程序运行正确。
文件Main.java

import java.util.Scanner;
public class Main {
public static void main(String args[]) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Point pt = 【代码1】 Point(1, n);// 创建对象
System.out.println(“pt.x=” + 【代码2】.x);// 取
pt.x = 5;// 修改
System.out.println(“pt.x=” + pt.x);
pt.move(3, 3);// 移动
System.out.println(“pt.x=” + pt.x);
System.out.println(“pt.y=” + pt.y);
【代码3】 pt2 = new Point();// 声明对象并new
System.out.println(“pt2.x=” + pt2.x);
pt2 = new Point(9, 2);
System.out.println(“pt2.x=” + pt2.x);
}
}
class Point {
int x, y;// 成员变量,属性
【代码4】 Point() {// 无参构造方法
x = 0;
y = 0;
}
public Point(int ix, int 【代码5】) {// 有参构造方法
x = ix;
y = iy;
}
【代码6】 move(int ix, int iy) {// 方法
x += ix;// x=x+ix
y += iy;
}
}

此题的上机步骤是:

  1. 建立一个Java项目,名称可以按题号取名;
  2. 建立一个类, 类的名称为Main。这一点非常重要;
  3. 将上面所有代码都复制到类Main中;
  4. 填代码;
  5. 提交代码,注意题号要一致。
    解答
    import java.util.Scanner;
    public class Main {
    public static void main(String args[]) {
    // TODO Auto-generated method stub
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    Point pt = new Point(1, n);// 创建对象
    System.out.println(“pt.x=” + pt.x);// 取
    pt.x = 5;// 修改
    System.out.println(“pt.x=” + pt.x);
    pt.move(3, 3);// 移动
    System.out.println(“pt.x=” + pt.x);
    System.out.println(“pt.y=” + pt.y);
    Point pt2 = new Point();// 声明对象并new
    System.out.println(“pt2.x=” + pt2.x);
    pt2 = new Point(9, 2);
    System.out.println(“pt2.x=” + pt2.x);
    }
    }
    class Point {
    int x, y;// 成员变量,属性
    Point() {// 无参构造方法
    x = 0;
    y = 0;
    }
    public Point(int ix, int iy) {// 有参构造方法
    x = ix;
    y = iy;
    }
    void move(int ix, int iy) {// 方法
    x += ix;// x=x+ix
    y += iy;
    }
    }

第2题(上机练习系统第47题 )
public class Main {
public static void main (String args[ ]) {
【代码1】//命令行窗口输出"教学活动从教室开始"
Teacher zhang = new Teacher();
Student jiang = 【代码2】Student();//创建对象
zhang.introduceSelf();
jiang.【代码2】; //调用它的方法
}
}
class Teacher {
void introduceSelf() {
【代码3】 //命令行窗口输出"我是李老师"
}
}
class Student {
void introduceSelf() {
【代码4】/ /命令行窗口输出"我是学生,名字是:奖励"
}
}

此题的上机步骤是:

  1. 建立一个Java项目,名称可以按题号取名;
  2. 建立一个类, 类的名称为Main。这一点非常重要;
  3. 填代码;
  4. 提交代码,注意题号要一致。

解答:
public class Main {
public static void main (String args[ ]) {
System.out.println(“教学活动从教室开始”);//命令行窗口输出"教学活动从教室开始"
Teacher zhang = new Teacher();
Student jiang = new Student();//创建对象
zhang.introduceSelf();
jiang.introduceSelf(); //调用它的方法
}
}
class Teacher {
void introduceSelf() {
System.out.println(“我是李老师”); //命令行窗口输出"我是李老师"
}
}

class Student {
void introduceSelf() {
System.out.println(“我是学生,名字是:奖励”);//命令行窗口输出"我是学生,名字是:奖励"
}
}

第3题(上机练习系统第48题 )
第48题 填写代码. 关于Point类的操作(2)(10分)
下面程序构造一个类来描述屏幕上的一个点,该类的构成包括点的 x 和 y 两个坐标,以及一些对点 进行的操作,包括:取得点的坐标值,对点的坐标进行赋值,编写应用程序生成该类的对象并 对其进行操作。
请将下面程序的【代码】替换为Java程序代码,使程序运行正确。
文件Main.java
public class Main {

public static void main(String[] args) {
Point Point1 = new Point(3, 4);
System.out.println(“Point1:” + “(” +
Point1.x + “,” + Point1.y + “)”);

Point Point2 = Point1.getPoint();
System.out.println(“Point2:” + “(” +
Point2.x + “,” + Point2.y + “)”);

Point 【代码1】 = new Point(5, 6);
Point1.setPoint(Point3);
System.out.println(“Point1:” + “(” +
Point1.x + “,” + Point1.y + “)”);
}
}

class Point {
int x, y;

public Point(int x, 【代码2】) {
this.x = x;
this.y = y;
}

【代码3】 Point getPoint() {
Point tempPoint = new Point(x, y);
return tempPoint;
}

public void setPoint(Point point) {
this.x = point.x;
【代码4】y = point.y;
}

}

此题的上机步骤是:

  1. 建立一个Java项目,名称可以按题号取名;
  2. 建立一个类, 类的名称为Main。这一点非常重要;
  3. 填代码;
  4. 提交代码,注意题号要一致。
    解答:
    public class Main {

public static void main(String[] args) {
Point Point1 = new Point(3, 4);
System.out.println(“Point1:” + “(” +
Point1.x + “,” + Point1.y + “)”);

Point Point2 = Point1.getPoint();
System.out.println(“Point2:” + “(” +
Point2.x + “,” + Point2.y + “)”);

Point Point3 = new Point(5, 6);
Point1.setPoint(Point3);
System.out.println(“Point1:” + “(” +
Point1.x + “,” + Point1.y + “)”);
}
}

class Point {
int x, y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}

public Point getPoint() {
Point tempPoint = new Point(x, y);
return tempPoint;
}

public void setPoint(Point point) {
this.x = point.x;
this.y = point.y;
}

}

第4题(上机练习系统第49题 )
第49题 填写代码. 关于Point类的操作(3)(10分)
下面程序构造一个类来描述屏幕上的一个点,该类的构成包括点的 x 和 y 两个坐标,以及一些对点 进行的操作,包括:取得点的坐标值,对点的坐标进行赋值,编写应用程序生成该类的对象并 对其进行操作。
请将下面程序的【代码】替换为Java程序代码,使程序运行正确。
文件Main.java

public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Point origin = new 【代码1】(10, 10);
origin.getPoint();
【代码2】.setPoint(20, 20);
origin.getPoint();
}

}

class Point {
private int x;
private int y;

public Point(int x, int y) {
this.x = x;
【代码3】y = y;
}

public void setPoint(int x1, int y1) {
x = x1;
y = y1;
}

public void getPoint() {
System.out.println("Point x: " + x + ",y: " + y);
}

}

此题的上机步骤是:

  1. 建立一个Java项目,名称可以按题号取名;
  2. 建立一个类, 类的名称为Main。这一点非常重要;
  3. 填代码;
  4. 提交代码,注意题号要一致。
    解答:
    public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Point origin = new Point(10, 10);
origin.getPoint();
origin.setPoint(20, 20);
origin.getPoint();
}

}

class Point {
private int x;
private int y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}

public void setPoint(int x1, int y1) {
x = x1;
y = y1;
}

public void getPoint() {
System.out.println("Point x: " + x + ",y: " + y);
}

}

第5题(上机练习系统第50题 )

第50题 编写部分代码. 按面向对象要求编程在Employee加入身份证(10分)

下面程序在Employee类中加入身份证信息,但类Employee中部分代码缺失.请编写程序代码,使程序运行正确。
具体要求, 修改Employee中5个参数的构造器为6个参数的构造器;缺少一个名称为getID()的方法.需要加入.

此题的上机步骤是:

  1. 建立一个Java项目,名称可以按题号取名;
  2. 建立一个类, 类的名称为Main。这一点非常重要;
  3. 将题中代码复制到Main.java中;
  4. 按要求编写代码,请注意已经有的代码不能修改,特别是class Main类中的代码不能修改;
  5. 提交代码,注意题号要一致。

文件Main.java

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner reader = new Scanner(System.in);
String name=reader.nextLine();
String ID=reader.nextLine();
int salary= reader.nextInt();
int year = reader.nextInt();
int month=reader.nextInt();
int day=reader.nextInt();
Employee e = new Employee(name,ID,salary,year,month,day);
e.raiseSalary(5);
System.out.println(“姓名:”+e. getName()+ " 身份证:" +e.getID()+" 工资:"+ e.getSalary());
System.out.println(“The Main class is end.”);
}
}

class Employee {
private String name;//私有域,姓名
private String ID;//私有域,身份证
private double salary;//私有域,工资
private Date hireDay; //私有域,录用日期
//构造方法
public Employee(String n, double s, int year, int month, int day)
{
name = n;//参数,(局部)变量
salary = s;//参数, (局部)变量
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
// GregorianCalendar uses 0 for January(1月份是0)
hireDay = calendar.getTime();
}

public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public Date getHireDay() {
return hireDay;
}
//"涨工资"计算
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}

}

输入:
姓名
身份证
工资
以空格分隔的日期,形式为YYYY MM DD
输出:
姓名:xxx 身份证:yyyyy 工资:d 这里xxx和yyyyy是字符串,d是double数
The Main class is end.

样例输入:
Romeo
430502199807101121
50000
2014 7 11
样例输出:
姓名:Romeo 身份证:430502199807101121 工资:52500.0
The Main class is end.
解答:
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner reader = new Scanner(System.in);
String name=reader.nextLine();
String ID=reader.nextLine();
int salary= reader.nextInt();
int year = reader.nextInt();
int month=reader.nextInt();
int day=reader.nextInt();
Employee e = new Employee(name,ID,salary,year,month,day);
e.raiseSalary(5);
System.out.println(“姓名:”+e. getName()+ " 身份证:" +e.getID()+" 工资:"+ e.getSalary());
System.out.println(“The Main class is end.”);
}
}

class Employee {
private String name;//私有域,姓名
private String ID;//私有域,身份证
private double salary;//私有域,工资
private Date hireDay; //私有域,录用日期
//构造方法
public Employee(String n, String i, double s, int year, int month, int day)
{
name = n;//参数,(局部)变量
ID = i;
salary = s;//参数, (局部)变量
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
// GregorianCalendar uses 0 for January(1月份是0)
hireDay = calendar.getTime();
}

public String getName() {
return name;
}
public String getID() {
return ID;
}
public double getSalary() {
return salary;
}
public Date getHireDay() {
return hireDay;
}
//"涨工资"计算
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}

}

第6题(上机练习系统第51题 )

第51题 代码填空. 通过类完成加法运算(10分)

此题的上机步骤是:

  1. 建立一个Java项目,名称可以按题号取名;
  2. 建立一个类, 类的名称为Main。这一点非常重要;
  3. 将题目中代码复制,然后填代码;
  4. 提交代码,注意题号要一致。

下列Java应用程序是用户从键盘输入个人信息,然后程序将个人信息输出。
请按程序实现要求,将下面程序的【代码】替换为Java程序代码。
文件Main.java

import java.util.Scanner;
public class Main {
public 【代码1】 void main(String[] args) {
System.out.println(【代码2】.add(5));//显示5+2的结果
Scanner reader=new Scanner(System.in);
double y=reader.nextDouble();
System.out.println(Plus.add(y));//显示1+y的结果
Plus aPlus=new Plus(3,4);
System.out.println(【代码3】.add());//显示3+4的结果
}
}
class Plus{
static int a=1,b=2;//域
int x,y;
public Plus() {
x=0;y=0;
}
public Plus(int x, int y) {
【代码4】.x = x;
this.y = y;
}
public static int add(int x){
return(x+b);
}
public 【代码5】 add(){
return(x+y);
}
public static double add(【代码6】 y){
return(a+y);
}
【代码7】 add(int x,int y){
return (x+y);
}
}
解答:
import java.util.Scanner;
public class di51ti {
public static void main(String[] args) {
System.out.println(Plus.add(5));//显示5+2的结果
Scanner reader=new Scanner(System.in);
double y=reader.nextDouble();
System.out.println(Plus.add(y));//显示1+y的结果
Plus aPlus=new Plus(3,4);
System.out.println(aPlus.add());//显示3+4的结果
}
}
class Plus{
static int a=1,b=2;//域
int x,y;
public Plus() {
x=0;y=0;
}
public Plus(int x, int y) {
this.x = x;
this.y = y;
}
public static int add(int x){
return(x+b);
}
public int add(){
return(x+y);
}
public static double add(double y){
return(a+y);
}
public static int add(int x,int y){
return (x+y);
}
}

第7题(上机练习系统第52题 )

第52题 填写代码,以Employee类为例的静态域和静态方法(10分)

以下程序是有关静态域和静态方法的例子,请将下面程序的【代码】替换为Java程序代码,使程序运行正确。
文件Main.java

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3];

  staff[0] = new Employee("Tom", 40000);
  staff[1] = new Employee("Dick", 60000);
  staff[2] = new Employee("Harry", 65000);
  Scanner reader=new Scanner(System.in);
  for (【代码1】 e: staff)
  {
int n=reader.nextInt();
e.setId(n);
System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
           + e.getSalary());
  }
  int n = 【代码2】.getNextId(); // calls static method
  System.out.println("Next available id=" + n);
  System.out.println("The Main class is end.");

}
}
class Employee
{
private 【代码3】 int nextId = 1;
private String name;
private double salary;
private int id;
public Employee(String n, double s)
{
name = n;
salary = s;
id = 0;
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public int getId()
{
return id;
}
public void setId(int 【代码4】)
{
id = nextId; // set id to next available id
nextId+=n;
}
public 【代码5】 int getNextId() //静态方法
{
return nextId; // returns static field
}
}

输入:
n个数
输出:
按程序运行结果
The Main class is end.

样例输入:
3 4 6
样例输出:
name=Tom,id=1,salary=40000.0
name=Dick,id=4,salary=60000.0
name=Harry,id=8,salary=65000.0
Next available id=14
The Main class is end.

解答:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3];

  staff[0] = new Employee("Tom", 40000);
  staff[1] = new Employee("Dick", 60000);
  staff[2] = new Employee("Harry", 65000);
  Scanner reader=new Scanner(System.in);
  for (Employee e: staff)
  {
	int n=reader.nextInt();
	e.setId(n);
	System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
           + e.getSalary());
  }
  int n = Employee.getNextId(); // calls static method
  System.out.println("Next available id=" + n);
  System.out.println("The Main class is end.");

}
}
class Employee
{
private static int nextId = 1;
private String name;
private double salary;
private int id;
public Employee(String n, double s)
{
name = n;
salary = s;
id = 0;
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public int getId()
{
return id;
}
public void setId(int n)
{
id = nextId; // set id to next available id
nextId+=n;
}
public static int getNextId() //静态方法
{
return nextId; // returns static field
}
}

猜你喜欢

转载自blog.csdn.net/xuanyulevel6/article/details/126068289