java实验(4)Java中类的继承及其应用(加强)-创新互联
一、实验内容
创新互联专注于大观网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供大观营销型网站建设,大观网站制作、大观网页设计、大观网站官网定制、小程序开发服务,打造大观网络公司原创品牌,更为您提供大观网站排名全网营销落地服务。1、假如我们在开发一个系统时需要对员工进行建模,【员工Employee】包含3个属性:姓名name、工号id以及工资salary;【经理Manager】也是员工,除了含有员工的属性外,另为还有一个奖金属性bonus。
请使用继承的思想设计出1个员工类和1个经理类,要求类中提供必要的方法进行属性访问,并能够一次性输出对象的基本信息。
输入描述:按照先输入员工类数据(name,id,salary)再经理类数据(name,id,salary,bonus)。
输入案例:
Zhangsan 20202001 8800
Lisi 20203001 9800 12000
输出案例:
Name: Zhangsan, Job Number: 20202001, Salary: 8800.0
Name: Lisi, Job Number: 20203001, Salary: 9800.0, Bonus: 12000.0
package com.HelloWorld;
import java.util.Scanner;
public class Test401 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Employee em=new Employee();
Manager ma=new Manager();
String name1=sc.next();
String id1=sc.next();
double salary1=sc.nextDouble();
String name2=sc.next();
String id2=sc.next();
double salary2=sc.nextDouble();
double bonus=sc.nextDouble();
em.setName(name1);
em.setId(id1);
em.setSalary(salary1);
ma.setName(name2);
ma.setId(id2);
ma.setSalary(salary2);
ma.setBonus(bonus);
em.out();
ma.out();
}
}
class Employee {
private String name;
private String id;
private double salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void out () {
System.out.println("Name: "+name+", Job Number: "+id+", Salary: "+salary);
}
}
class Manager extends Employee{
private double bonus;
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
public void out () {
System.out.println("Name: "+getName()+", Job Number: "+getId()+", Salary: "+ getSalary()+", Bonus: "+getBonus());
}
}
2、编写一个Java程序,该程序包括3个类:Monkey类、People类和测试类Test。最低要求如下:
(1) Monkey类中有个构造方法:Monkey (String name),并且有个public void speak()方法,在speak方法中输出“YiYiYaYa......”的信息。
(2)People类是Monkey类的子类,在People类中的speak方法中输出“Hey you~~, not bad! You can speak!”的信息。
(3)在People类中新增方法void think(),在think方法中输出“Stop talking! Think seriously!”的信息。
(4)在测试类Test的main方法中创建Monkey与People类的对象类测试这2个类的功能。
输入描述:先输入Monkey类再输入People类。
输入案例:
Toney
Tom
输出案例:
I am Monkey Toney
YiYiYaYa......
I am People Tom
Hey you~~, not bad! You can speak!
Stop talking! Think seriously!
package com.HelloWorld;
import java.util.Scanner;
public class Test402 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String m=sc.next();
String p=sc.next();
Monkey monkey=new Monkey(m);
monkey.speak();
System.out.println();
People people=new People(p);
people.speak();
people.think();
System.out.println();
}
}
class Monkey {
Monkey() {
}
Monkey(String name) {
System.out.println("I am Monkey "+name);
}
public void speak() {
System.out.println("YiYiYaYa......");
}
}
class People extends Monkey{
People(String name){
System.out.println("I am People "+name);
}
public void speak() {
System.out.println("Hey you~~, not bad! You can speak!");
}
void think(){
System.out.println("Stop talking! Think seriously!");
}
}
3、编写一个Java程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader,及判断超载的方法isLoader(),默认最多载人数为5。卡车类Truck是Car类的子类,其中包含的属性有载重量payload,且默认最多载人数为2,载重量为1000。每个类都应有构造方法及输出相关数据的方法,最后写一个测试类来测试这些类的功能。测试类要求2个汽车类和2个Vehicle类对象。
输入描述:按照(wheels,weight,loader,payload)属性顺序输入对象。
输入案例:
4 600 4
4 600 6
6 800 2 800
12 1000 3 1200
输出案例;
There is a car,
the number of wheels is 4, the weight is 600.0
this car carries 4 passengers!
There is another car,
the number of wheels is 4, the weight is 600.0
this car is overloaded!
There is a truck,
the number of wheels is 6, the weight is 800.0
this truck has 2 passengers,
this truck carries 800.0
There is another truck,
the number of wheels is 12, the weight is 1000.0
this truck is overloaded!
this truck is overweight!
package com.HelloWorld;
import java.util.Scanner;
public class Test403 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Car c1=new Car();
Car c2=new Car();
Truck t1=new Truck();
Truck t2=new Truck();
int wh1=sc.nextInt();
double w1=sc.nextDouble();
int p1=sc.nextInt();
int wh2=sc.nextInt();
double w2=sc.nextDouble();
int p2=sc.nextInt();
int wh3=sc.nextInt();
double w3=sc.nextDouble();
int p3=sc.nextInt();
double pay1=sc.nextDouble();
int wh4=sc.nextInt();
double w4=sc.nextDouble();
int p4=sc.nextInt();
double pay2=sc.nextDouble();
System.out.println("There is a car,");
c1.setWheels(wh1);
c1.setWeight(w1);
c1.out();
c1.setLoader(5);
c1.isLoader(p1);
System.out.println();
System.out.println("There is another car,");
c2.setWheels(wh2);
c2.setWeight(w2);
c2.out();
c2.setLoader(5);
c2.isLoader(p2);
System.out.println();
System.out.println("There is a truck,");
t1.setWheels(wh3);
t1.setWeight(w3);
t1.out();
t1.setLoader(2);
t1.isLoader(p3);
t1.ispayload(pay1);
System.out.println();
System.out.println("There is another truck,");
t2.setWheels(wh4);
t2.setWeight(w4);
t2.out();
t2.setLoader(2);
t2.isLoader(p4);
t2.ispayload(pay2);
System.out.println();
}
}
class Vehicleone {
private double weight;
private int wheels;
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public int getWheels() {
return wheels;
}
public void setWheels(int wheels) {
this.wheels = wheels;
}
public void out() {
System.out.println("the number of wheels is "+wheels+", the weight is "+weight);
}
}
class Car extends Vehicleone {
private int loader;
public int getLoader() {
return loader;
}
public void setLoader(int number) {
this.loader=number;
}
public void isLoader(int loader) {
if(this.loader>=loader)
System.out.println("this car carries "+loader+" passengers!");
else System.out.println("this car is overloaded!");
}
}
lass Truck extends Car{
private double payload=1000;
public double getPayload() {
return payload;
}
public void setPayload(double payload) {
this.payload = payload;
}
public void isLoader(int loader) {
if(getLoader()>=loader)
System.out.println("this truck has "+loader+" passengers,");
else System.out.println("this truck is overloaded!");
}
public void ispayload(double payload) {
if(this.payload>=payload)
System.out.println("this truck carries "+payload);
else System.out.println("this truck is overweight!");
}
}
二、实验结果与分析(包括:输入数据、输出数据、程序效率及正确性等)(此处写清题号与其答案,可截图)
程序运行结果截图如下:
程序运行结果截图如下:
3、程序运行结果截图如下:
三、问题与讨论
Java中输入
1、 读取输入
1)构建一个Scanner,附属到System.in
Scanner in = new Scanner(System.in);
此时可以使用Scanner类的各种方法来读取输入。例如:nextLine方法来读取一行输入
System.out.print("What is your name?");
String name = in.nextLine();
nextLine(): 读取的一行中可能包含空格
next(): 读取单个单词。
nextInt(): 读取一个整数。
nextDouble():读取一个浮点数
例如:String firstName = in.next();
int age = in.nextInt();
Scanner类包含在java.util包中,因此在使用时,在程序开始处,输入import java.util.*;
注:Scanner不适合用于从终端读取密码,因为输入的文本对于任何人是可见的。在Java SE6中引入了Console类用于该目的。用如下代码来读取密码:
Console cons = System.console();
String username = cons.readLine("User Name:");
char[] passwd = cons.readPassword("Password: ");
为安全起见,password返回的是一个字符数组,而不是一个字符串。
Console对象用于输入处理不像Scanner一样方便。Console对象一次只能读取一行。没有其他方法用于读取单个单词或数字。
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
分享名称:java实验(4)Java中类的继承及其应用(加强)-创新互联
分享网址:http://azwzsj.com/article/dsdoci.html