update,
This commit is contained in:
22
_resources/it114105/itp4510/Lab01/Lab1.2/q1/Coffee.java
Normal file
22
_resources/it114105/itp4510/Lab01/Lab1.2/q1/Coffee.java
Normal 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);
|
||||
}
|
||||
}
|
19
_resources/it114105/itp4510/Lab01/Lab1.2/q1/Drink.java
Normal file
19
_resources/it114105/itp4510/Lab01/Lab1.2/q1/Drink.java
Normal 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;
|
||||
}
|
||||
}
|
25
_resources/it114105/itp4510/Lab01/Lab1.2/q1/Food.java
Normal file
25
_resources/it114105/itp4510/Lab01/Lab1.2/q1/Food.java
Normal 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;
|
||||
}
|
||||
}
|
12
_resources/it114105/itp4510/Lab01/Lab1.2/q1/TestFood.java
Normal file
12
_resources/it114105/itp4510/Lab01/Lab1.2/q1/TestFood.java
Normal 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]);
|
||||
}
|
||||
}
|
||||
}
|
12
_resources/it114105/itp4510/Lab01/Lab1.2/q2/Circle.java
Normal file
12
_resources/it114105/itp4510/Lab01/Lab1.2/q2/Circle.java
Normal 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;
|
||||
}
|
||||
}
|
9
_resources/it114105/itp4510/Lab01/Lab1.2/q2/Shape.java
Normal file
9
_resources/it114105/itp4510/Lab01/Lab1.2/q2/Shape.java
Normal 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
|
||||
}
|
12
_resources/it114105/itp4510/Lab01/Lab1.2/q2/Square.java
Normal file
12
_resources/it114105/itp4510/Lab01/Lab1.2/q2/Square.java
Normal 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;
|
||||
}
|
||||
}
|
@@ -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
|
||||
}
|
||||
}
|
34
_resources/it114105/itp4510/Lab01/Lab1.2/q3/Circle.java
Normal file
34
_resources/it114105/itp4510/Lab01/Lab1.2/q3/Circle.java
Normal 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;
|
||||
}
|
||||
}
|
24
_resources/it114105/itp4510/Lab01/Lab1.2/q3/Point.java
Normal file
24
_resources/it114105/itp4510/Lab01/Lab1.2/q3/Point.java
Normal 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 + "]";
|
||||
}
|
||||
}
|
40
_resources/it114105/itp4510/Lab01/Lab1.2/q3/Rectangle.java
Normal file
40
_resources/it114105/itp4510/Lab01/Lab1.2/q3/Rectangle.java
Normal 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;
|
||||
}
|
||||
}
|
13
_resources/it114105/itp4510/Lab01/Lab1.2/q3/Shape.java
Normal file
13
_resources/it114105/itp4510/Lab01/Lab1.2/q3/Shape.java
Normal 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;
|
||||
}
|
||||
}
|
12
_resources/it114105/itp4510/Lab01/Lab1.2/q3/TestShape.java
Normal file
12
_resources/it114105/itp4510/Lab01/Lab1.2/q3/TestShape.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user