This commit is contained in:
louiscklaw
2025-02-01 01:58:47 +08:00
parent b3da7aaef5
commit 04dbefcbaf
1259 changed files with 280657 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
public class CommissionWorker extends Worker{
private double commission;
private int quantity;
public CommissionWorker(String name, double c, int q) {
super(name);
setCommission(c);
setQuantity(q);
}
public void setCommission(double c) {
commission = c;
}
public void setQuantity(int q) {
quantity = q;
}
public double earnings() {
salary = commission * quantity;
return salary;
}
public String toString() {
return super.toString() + " earned commission of $" +
earnings();
}
}

View File

@@ -0,0 +1,30 @@
public class HourlyWorker extends Worker {
private double wage;
private double hours;
public HourlyWorker(String name, double w, double h) {
super(name);
setWage(w);
setHours(h);
}
public void setWage(double w) {
wage = w;
}
public void setHours(double h) {
hours = h;
}
public double earnings() {
salary = wage * hours;
return salary;
}
public String toString() {
return super.toString() + " earned $" + earnings() +
" for " + hours + " hours";
}
}

View File

@@ -0,0 +1,10 @@
public class TestWorker {
public static void main(String [] args) {
Worker [] w = new Worker[3];
w[0] = new Worker("Peter");
w[1] = new CommissionWorker("John", 120, 10);
w[2] = new HourlyWorker("Mary", 25, 40);
for( int i = 0; i < w.length; i++)
System.out.println(w[i]);
}
}

View File

@@ -0,0 +1,16 @@
public class Worker {
private String name;
protected double salary;
public Worker(String name) {
this.name = name;
}
public double earnings() {
return salary;
}
public String toString() {
return name;
}
}

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());
}
}
}

View File

@@ -0,0 +1,25 @@
public class PartTimeStaff extends Staff implements Salary {
private int workingHour;
public PartTimeStaff(String name, int id, char grade, int workingHour) {
super(name, id, grade);
this.workingHour = workingHour;
}
public void display() {
System.out.println("Name: " + name + "; ID: " + id + "; Grade: " + grade + "; Working Hour: " + workingHour + "; Salary: " + computeSalary());
}
public int computeSalary() {
switch (grade) {
case 'A':
return SALARY_A;
case 'B':
return SALARY_B;
case 'C':
return SALARY_C;
default:
return SALARY_OTHER;
}
}
}

View File

@@ -0,0 +1,8 @@
public interface Salary {
public final int SALARY_A = 4000;
public final int SALARY_B = 3000;
public final int SALARY_C = 2000;
public final int SALARY_OTHER = 1000;
public abstract int computeSalary();
}

View File

@@ -0,0 +1,13 @@
public abstract class Staff {
protected String name;
protected int id;
protected char grade;
public Staff(String name, int id, char grade) {
this.name = name;
this.id = id;
this.grade = grade;
}
public abstract void display();
}

View File

@@ -0,0 +1,8 @@
public class TestStaff {
public static void main(String args[]) {
PartTimeStaff p1 = new PartTimeStaff("John", 123, 'B', 20);
PartTimeStaff p2 = new PartTimeStaff("Mary", 124, 'A', 22);
p1.display();
p2.display();
}
}