目录
- 第51题 代码填空. 通过类完成加法运算(10分)
- 🍋题目描述
- 🍋源代码
- 第52题 填写代码,以Employee类为例的静态域和静态方法(10分)
- 🍋题目描述
- 🍋源代码
第51题 代码填空. 通过类完成加法运算(10分)
🍋题目描述
此题的上机步骤是:
- 建立一个Java项目,名称可以按题号取名;
- 建立一个类, 类的名称为Main。这一点非常重要;
- 将题目中代码复制,然后填代码;
- 提交代码,注意题号要一致。
下列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 Main {
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);
}
}
第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
}
}