题目一
实验内容
编写一个程序,程序包括如下内容
- 实现父类一个动物的类,包括成员变量 名字 年龄 皮毛颜色,带参数构造函数,动物类有一个方法,move,打印动物是可以动的
- 实现一个子类老鼠的类,继承动物类,老鼠类继承父类成员变量,老鼠还有个自己的属性,体重,实现老鼠类构造函数继承,move方法重写,老鼠是可以跑的,再实现一个方法,老鼠惨叫
- 实现一个子类狗的类,继承动物类,狗类继承父类成员变量,狗类还有个自己的属性,体重,实现狗类构造函数继承,实现狗抓老鼠方法,可以抓到也可以抓不到,抓到老鼠惨叫,抓不到老鼠跑掉
- 主类实现一个老鼠jack,一个狗xiaobai,实现狗抓老鼠的过程
实验代码
父类(动物类)
public class Animal {
public String name;
public int age;
public String color;
public Animal(String name,int age,String color) {
this.name = name;
this.age = age;
this.color = color;
}
public void Move() {
System.out.println("动物是可以动的");
}
}
子类(狗类🐕):
public class Dog extends Animal {
float kg;
public Dog(String name,int age,String color,float kg) {
super(name,age,color);
this.kg = kg;
}
public void Catch() {
System.out.println("狗已经抓到老鼠了");
}
}
子类(老鼠类🐀):
public class Mouse extends Animal {
float kg;
public Mouse(String name,int age,String color,float kg) {
super(name,age,color);
this.kg = kg;
}
public void Move() {
System.out.println("老鼠是可以跑的");
}
public void Voice() {
System.out.println("老鼠惨叫");
}
}
测试类:
public class TestAnimal {
public static void main(String[] args) {
Dog d = new Dog("xiaobai",1,"black",2);
Mouse m = new Mouse("jack",3,"white",3);
m.Move();
d.Catch();
m.Voice();
}
}
实验结果
题目二
实验内容
开一家文体用品店shop,包括店名,店长名,资金
- 体育用品sportsgoods,包括产品包括名称,厂家,型号,定价,销价
- 办公用品officesupplies 包括产品包括名称,厂家,型号,定价,销价
- 实现实体店一家
- 体育用品实现一个继承类: 跑步机
- 办公用品实现一个继承类: 打印机
- 资金量充足,实现进货(跑步机,打印机),销货(跑步机,打印机),实现
- 对资金的操作
商店:
public class Shop {
String shopname;
String manager;
double fund;
public Shop(String shopname,String manager,double fund) {
this.shopname = shopname;
this.manager = manager;
this.fund = fund;
}
public void shopping() {
System.out.println("欢迎光临"+shopname);
System.out.println("店长:"+manager);
}
}
办公用品officesupplies:
public class officesupplies {
String product_name;
String factory;
int model;
float pricing;
float selling_price;
public officesupplies(String product_name,String factory,int model,float pricing,float selling_price) {
super();
}
public void show2() {
System.out.println("商品名:"+product_name);
System.out.println("厂家:"+factory);
System.out.println("型号:"+model);
System.out.println("定价:"+pricing);
System.out.println("售价:"+selling_price);
}
}
体育用品sportsgoods:
public class sportsgoods {
String product_name;
String factory;
int model;
float pricing;
float selling_price;
public sportsgoods(String product_name,String factory,int model,float pricing,float selling_price) {
super();
}
public void show1() {
System.out.println("商品名:"+product_name);
System.out.println("厂家:"+factory);
System.out.println("型号:"+model);
System.out.println("定价:"+pricing);
System.out.println("售价:"+selling_price);
}
}
打印机(printer):
public class Printer extends officesupplies {
public Printer(String product_name, String factory, int model, float pricing, float selling_price) {
super(product_name, factory, model, pricing, selling_price);
}
}
跑步机(treadmill):
public class Treadmill extends sportsgoods {
public Treadmill(String product_name, String factory, int model, float pricing, float selling_price) {
super(product_name, factory, model, pricing, selling_price);
}
}
测试类:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
double buy_money;
double sel_money;
Shop market= new Shop("Gym", "张发财", 1000000);
Treadmill run = new Treadmill("run", "nice", 2, 1500, 3000);
run.show1();
Printer print = new Printer("printer", "good", 9, 250, 500);
print.show2();
while(true) {
System.out.println("请输入你要进行的操作:(1进货,2销售,3查看店内资金,4退出)");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
switch(n) {
case 1:
System.out.println("请选择你要进的货物:(1跑步机,2打印机,3跑步机+打印机)");
int a = sc.nextInt();
if(a==1) {
System.out.println("请问你要进多少跑步机");
int b = sc.nextInt();
buy_money = 1500*b;
if(buy_money<market.fund) {
market.fund -= buy_money;
System.out.println("恭喜你,购买成功!\n共消费"+buy_money+"元");
}else {
System.out.println("资金不足无法购买!");
}
}else if(a==2){
System.out.println("请问你要进多少打印机");
int c = sc.nextInt();
buy_money = 250*c;
if(buy_money<market.fund) {
market.fund -= buy_money;
System.out.println("恭喜你,购买成功!\n共消费"+buy_money+"元");
}else {
System.out.println("资金不足无法购买!");
}
}else if(a==3) {
System.out.println("请问你要进多少跑步机和打印机");
int d = sc.nextInt();
int e = sc.nextInt();
buy_money = 1500*d + 250*e;
if(buy_money<market.fund) {
market.fund -= buy_money;
System.out.println("恭喜你,购买成功!\n共消费"+buy_money+"元");
}else {
System.out.println("资金不足无法购买!");
}
}else {
System.out.println("输入错误");
}
break;
case 2:
System.out.println("请选择你要卖的货物:(1跑步机,2打印机,3跑步机+打印机)");
int x = sc.nextInt();
if(x==1) {
System.out.println("请问你销售了多少跑步机");
int y = sc.nextInt();
sel_money = 3000*y;
market.fund += sel_money;
System.out.println("恭喜你,购买成功!\n共赚取"+sel_money+"元");
}else if(x==2){
System.out.println("请问你销售了多少打印机");
int z = sc.nextInt();
sel_money = 500*z;
market.fund += sel_money;
System.out.println("恭喜你,购买成功!\n共赚取"+sel_money+"元");
}else if(x==3) {
System.out.println("请问你销售了多少跑步机和打印机");
int z1 = sc.nextInt();
int z2 = sc.nextInt();
sel_money = 3000*z1+500*z2;
market.fund += sel_money;
System.out.println("恭喜你,购买成功!\n共赚取"+sel_money+"元");
}else {
System.out.println("输入错误");
}
break;
case 3:
System.out.println("店内资金剩余:"+market.fund+"元");
if(n==4) {
break;
}
}
}
}
}