目录
- 第49题 填写代码. 关于Point类的操作(3)(10分)
- 🍋题目描述
- 🍋源代码
- 第50题 编写部分代码. 按面向对象要求编程在Employee加入身份证(10分)
- 🍋题目描述
- 🍋源代码
第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);
}
}
此题的上机步骤是:
- 建立一个Java项目,名称可以按题号取名;
- 建立一个类, 类的名称为Main。这一点非常重要;
- 填代码;
- 提交代码,注意题号要一致。
🍋源代码
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);
}
}
第50题 编写部分代码. 按面向对象要求编程在Employee加入身份证(10分)
🍋题目描述
下面程序在Employee类中加入身份证信息,但类Employee中部分代码缺失.请编写程序代码,使程序运行正确。
具体要求, 修改Employee中5个参数的构造器为6个参数的构造器;缺少一个名称为getID()的方法.需要加入.
此题的上机步骤是:
- 建立一个Java项目,名称可以按题号取名;
- 建立一个类, 类的名称为Main。这一点非常重要;
- 将题中代码复制到Main.java中;
- 按要求编写代码,请注意已经有的代码不能修改,特别是class Main类中的代码不能修改;
- 提交代码,注意题号要一致。
文件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;
}
}