Java基础之模拟披萨店


实验内容

题目一

抽象类:编写一个程序,实现模拟开披萨店

  • 定义一个抽象类,Pizza,包括成员变量,名字,口味,辅料,实现构造函数,实现制作Pizza的步骤,包括准备,和面,烘烤,切片,后两个是抽象方法。
  • 定义一个抽象类,PizzaStore,实现定披萨的方法orderPizza,传入Pizza的名字,返回Pizza,定义一个抽象方法,createPizza,在orderPizza中调用,实现不可以预定并制作不同口味的pizza。
  • 分别定义两个Pizza的子类继承Pizza,并分别实现抽象方法
  • 定义一个PizzaStore的子类继承PizzaStore,实现抽象方法
  • 主类:生成两款Pizza,和一个PizzaStore店对象,实现购买Pizza

实验代码

披萨:

package ShiYan5;
abstract public class Pizza {
	public String name;
	public String taste;
	public String accessories;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTaste() {
		return taste;
	}
	public void setTaste(String taste) {
		this.taste = taste;
	}
	public String getAccessories() {
		return accessories;
	}
	public void setAccessories(String accessories) {
		this.accessories = accessories;
	}
	public void Prepare() {
		System.out.println("正在准备ing");
	}
	public void Mix() {
		System.out.println("正在和面");
	}
	public abstract void Bake();
	public abstract void Slicing();
}

披萨店:

package ShiYan5;
abstract public class PizzaStore {
	public Pizza OrderPizza(String type) {
		Pizza pz = CreatePizza(type);
		pz.Prepare();
		pz.Mix();
		pz.Bake();
		pz.Slicing();
		return pz;
	}
	public abstract Pizza CreatePizza(String type);
}

芝士披萨:

public class CheesePizza extends Pizza {
	public CheesePizza() {
		name = "芝士披萨";
		taste = "甜";
		accessories = "芝士";
	}
	@Override
	public void Bake() {
		// TODO Auto-generated method stub
		System.out.println("正在烘焙,请稍等30min");
	}
	@Override
	public void Slicing() {
		// TODO Auto-generated method stub
		System.out.println("切8份");
	}
}

烤肉披萨:

public class BarbecuePizza extends Pizza {
	public BarbecuePizza() {
		name = "烤肉披萨";
		taste = "咸";
		accessories = "肉";
	}
	@Override
	public void Bake() {
		// TODO Auto-generated method stub
		System.out.println("正在烘焙,请稍等40min");
	}
	@Override
	public void Slicing() {
		// TODO Auto-generated method stub
		System.out.println("切成10份");
	}
}

烤肉披萨店(继承披萨店):

public class BarbecuePizzaStore extends PizzaStore {
	@Override
	public Pizza CreatePizza(String type) {
		if(type.equals("芝士披萨")) {
			CheesePizza cp = new CheesePizza();
			return cp;
		}
		else if(type.equals("烤肉披萨")) {
			BarbecuePizza bp = new BarbecuePizza();
			return bp;
		}
		return null;
	}
}

测试类:

import java.util.Scanner;
public class Test {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner type = new Scanner(System.in);
		BarbecuePizzaStore pz = new BarbecuePizzaStore();
		System.out.println("请输入您所需的披萨");
		System.out.println("1:芝士披萨");
		System.out.println("2:烤肉披萨");
		int b = type.nextInt();
		String a = null;
		if(b == 1) {
			a = "芝士披萨";
		}else if(b == 2) {
			a = "烤肉披萨";
		}else {
			System.out.println("请重新输入");
		}
		pz.OrderPizza(a);
	}
}

实验结果

在这里插入图片描述

题目二

多态:编写一个程序,程序包括如下内容

  • 定义一个Animal的类,成员变量包括名字,年龄,皮毛肤色,实现构造函数,实现成员函数Enjoy,打印动物是可以高兴的
  • 定义一个Cat的类,继承Animal的类,继承构造函数,并且重写成员函数,打印猫高兴了要喵喵叫
  • 定义一个Dog的类,继承Animal的类,继承构造函数,并且重写成员函数,打印狗高兴了要旺旺叫
  • 定义一个Lady的类,成员变量包括名字,年龄和宠物Animal,实现构造函数,实现成员函数MyPetEnjoy,逗宠物高兴
  • 主类:实现一个Dog的对象,一个Cat对象,实现Lady对象,实现Lady逗宠物猫和狗。

实验代码

宠物大类:

package petpg;
import foodpg.Food;
public class Pet {
	private String name;
	private String color;
	private int age;
	public Pet(String name, String color, int age) {
		super();
		this.name = name;
		this.color = color;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void enjoy() {
		System.out.println("我很高兴");
	}
}

狗🐕:继承宠物:

public class Dog extends Pet{
	public Dog(String name, String color, int age) {
		super(name, color, age);
		// TODO Auto-generated constructor stub
	}
	public void enjoy() {
		System.out.println("我很高兴,汪汪叫");
	}
}

猫🐱:继承宠物:

public class Cat extends Pet{
	public Cat(String name, String color, int age) {
		super(name, color, age);
		// TODO Auto-generated constructor stub
	}
	public void enjoy() {
		System.out.println("我很高兴,喵喵叫");
	}
}

lady🚺:

public class Lady {
	String name;
	int age;
	String Animal;
	public Lady(String name, int age, String animal) {
		super();
		this.name = name;
		this.age = age;
		this.Animal = animal;
	}
	public void MyPetEnjoy(Pet p) {
		p.enjoy();
	}
}

测试类:

public class Test {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Cat mm=new Cat("小白", "黑色",1);
		Dog d=new Dog("小黑", "白色",2);
		Lady l=new Lady("动物", 20, "小白和小黑");
		l.MyPetEnjoy(mm);
		l.MyPetEnjoy(d);
	}
}

实验结果

在这里插入图片描述

题目三

接口:编写一个程序,程序包含如下内容

  • 定义一个含计算面积和周长方法的图形接口shape(area,length)
  • 编写实现图形接口的半圆类。
  • 编写实现图形接口的长方体类。
  • 主类里面去生成具体对象实现。

实验代码

接口:

public interface Shape {
	public double area();
	public double length();
}

⚪类

public class Circle implements Shape {
	double r;
	public Circle(double r) {
		super();
		this.r = r;
	}
	@Override
	public double area() {

		return r*r*3.14/2;
	}
	@Override
	public double length() {
		return 3.14*r+2*r;
	}
}

三角形

public class Rectangular implements Shape {
	double length;
	double width;
	double high;
	public Rectangular(double length, double width, double high) {
		super();
		this.length = length;
		this.width = width;
		this.high = high;
	}
	@Override
	public double area() {
		return length*high*2+high*width*2+width*length*2;
	}
	@Override
	public double length() {
		return length+width+high;
	}
}

测试类:

import java.util.Scanner;
public class Test1 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入半圆的半径");
		Circle c = new Circle(sc.nextDouble());
		System.out.println("请输入长方体的长宽高");
		Rectangular r = new Rectangular(sc.nextDouble(),sc.nextDouble(),sc.nextDouble());
		System.out.println("半圆的面积是:"+c.area());
		System.out.println("半圆的周长是:"+c.length());
		System.out.println("长方形的面积为:"+r.area());
		System.out.println("长方形的周长是:"+r.length());
	}
}

实验结果

在这里插入图片描述

  • 6
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值