This commit is contained in:
louiscklaw
2025-01-31 19:15:17 +08:00
parent 09adae8c8e
commit 6c60a73f30
1546 changed files with 286918 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
public class Coffee extends Drink {
protected boolean isSweet;
public Coffee(String name, int price, int volume, boolean isSweet) {
super(name, price, volume);
this.isSweet = isSweet;
}
public String toString() {
if (isSweet)
return super.toString() + ", sweet";
else
return super.toString() + ", not sweet";
}
public void setPrice(int price) {
if (price < 10)
this.price = 10;
else
super.setPrice(price);
}
}

View File

@@ -0,0 +1,19 @@
public class Drink extends Food {
protected int volume;
public Drink(String name, int price, int volume) {
super(name, price);
this.volume = volume;
}
public void setPrice(int price) {
if (price < 5)
this.price = 5;
else
this.price = price;
}
public String toString() {
return super.toString() + ", volume=" + volume;
}
}

View File

@@ -0,0 +1,25 @@
public class Food {
protected String name;
protected int price;
public Food() {
name = null;
price = 0;
}
public Food(String name, int price) {
this.name = name;
setPrice(price);
}
public String toString() {
return "name=" + name + ", price=" + price;
}
public void setPrice(int price) {
if (price >= 0)
this.price = price;
else
this.price = 0;
}
}

View File

@@ -0,0 +1,12 @@
public class TestFood {
public static void main(String s[]) {
Food f[] = new Food[4];
f[0] = new Drink("Pepsi", 7, 250);
f[1] = new Coffee("Cappuccino", 13, 200, true);
f[2] = new Drink("Orange Juice", -10, 180);
f[3] = new Coffee("Ireland", -11, 200, false);
for (int i = 0; i < f.length; i++) {
System.out.println("Food " + i + ": " + f[i]);
}
}
}

View File

@@ -0,0 +1,12 @@
public class Circle extends Shape { // line 10
private int radius; // line 11
public Circle(String name, int r) {
super(name); // line 12
radius = r; // line 13
}
public double area() {
return radius * radius * 3.1416;
}
}

View File

@@ -0,0 +1,9 @@
public abstract class Shape { // line 1
private String name; // line 2
public Shape(String name) { // line 3
this.name = name; // line 4
}
public abstract double area(); // line 5
}

View File

@@ -0,0 +1,12 @@
public class Square extends Shape { // line 6
private int side; // line 7
public Square(String name, int side) {
super(name); // line 8
this.side = side; // line 9
}
public double area() {
return side * side;
}
}

View File

@@ -0,0 +1,8 @@
public class TestShape {
public static void main(String[] args) { // line 14
Shape s1 = new Square("Square1", 4); // line 15
Shape s2 = new Circle("Circle1", 2); // line 16
System.out.println("Area of circle: " + s2.area()); // line 17
System.out.println("Area of square: " + s1.area()); // line 18
}
}

View File

@@ -0,0 +1,34 @@
public class Circle extends Shape{
private double radius;
private Point center;
public Circle(double radius, double x, double y){
super("circle");
this.radius = radius;
center = new Point(x, y);
}
public double getRadius(){
return radius;
}
public Point getCenter(){
return center;
}
public void setRadius(double radius){
this.radius = radius;
}
public void setCenter(Point center){
this.center = center;
}
public double getArea(){
return radius*radius*3.1416;
}
public String toString(){
return "center=" + center.toString() + "; radius=" + radius;
}
}

View File

@@ -0,0 +1,24 @@
public class Point {
private double x, y;
public Point(double a, double b) {
setPoint(a, b);
}
public void setPoint(double a, double b) {
x = a;
y = b;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public String toString() {
return "[" + x + ", " + y + "]";
}
}

View File

@@ -0,0 +1,40 @@
public class Rectangle extends Shape{
private Point topLeft;
private double width;
private double height;
public Rectangle(double width, double height, double x, double y) {
super("rectangle");
topLeft = new Point(x, y);
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public Point getTopLeft() {
return topLeft;
}
public void setWidth(double width) {
this.width = width;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea() {
return width * height;
}
public String toString() {
return "top left=" + topLeft.toString() + "; width=" + width + "; height=" + height;
}
}

View File

@@ -0,0 +1,13 @@
public abstract class Shape {
protected String name;
public Shape(String n) {
name = n;
}
public abstract double getArea();
public String getName() {
return name;
}
}

View File

@@ -0,0 +1,12 @@
public class TestShape {
public static void main(String[] args) {
Shape[] myShapes = new Shape[3];
myShapes[0] = new Circle(10.5, 20, 25);
myShapes[1] = new Rectangle(30.5, 23.5, 15.5, 20.5);
myShapes[2] = new Circle(8, 9.5, 10.5);
for (int i = 0; i < myShapes.length; i++) {
System.out
.println(myShapes[i].getName() + "=" + myShapes[i].toString() + "; Area=" + myShapes[i].getArea());
}
}
}