Adding early CS challenge solutions
This commit is contained in:
33
CS7 - Challenge Solution/Ball.java
Normal file
33
CS7 - Challenge Solution/Ball.java
Normal file
@@ -0,0 +1,33 @@
|
||||
public class Ball {
|
||||
private double diameter;
|
||||
private String color;
|
||||
private int timesBounced;
|
||||
|
||||
public Ball(double newDiameter, String newColor) {
|
||||
diameter = newDiameter;
|
||||
color = newColor;
|
||||
timesBounced = 0;
|
||||
}
|
||||
|
||||
public double getDiameter() {
|
||||
return diameter;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public int getTimesBounced() {
|
||||
return timesBounced;
|
||||
}
|
||||
|
||||
public void bounceBall() {
|
||||
timesBounced++;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "This ball is " + color + " with a diameter of " +
|
||||
diameter + " and has been bounced " + timesBounced +
|
||||
" times!";
|
||||
}
|
||||
}
|
||||
15
CS7 - Challenge Solution/Main.java
Normal file
15
CS7 - Challenge Solution/Main.java
Normal file
@@ -0,0 +1,15 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Ball ball1 = new Ball(5, "Red");
|
||||
Ball ball2 = new Ball(1, "Yellow");
|
||||
|
||||
for(int i = 0; i < 10; i++) {
|
||||
ball1.bounceBall();
|
||||
}
|
||||
|
||||
ball2.bounceBall();
|
||||
|
||||
System.out.println(ball1);
|
||||
System.out.println(ball2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user