Adding the comments for the early CS's
This commit is contained in:
@@ -1,30 +1,52 @@
|
||||
//A class based representation of a ball
|
||||
public class Ball {
|
||||
//3 instance variables, the first, diameter,
|
||||
//stores the diameter of the ball in whatever
|
||||
//units you want.
|
||||
//The second, a String representation of the color
|
||||
//of the ball.
|
||||
//The third, an integer that stores the number
|
||||
//of times the ball has been bounced.
|
||||
private double diameter;
|
||||
private String color;
|
||||
private int timesBounced;
|
||||
|
||||
//Constructor to create balls with, specifying
|
||||
//the diameter and color of the ball.
|
||||
//Note that timesBounced is set to 0,
|
||||
//this constructor assumes that this is a fresh
|
||||
//ball that has never been bounced.
|
||||
public Ball(double newDiameter, String newColor) {
|
||||
diameter = newDiameter;
|
||||
color = newColor;
|
||||
timesBounced = 0;
|
||||
}
|
||||
|
||||
//Returns the current diameter of the ball
|
||||
public double getDiameter() {
|
||||
return diameter;
|
||||
}
|
||||
|
||||
//Returns the current color of the ball
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
//Returns the number of times this ball has
|
||||
//been bounced
|
||||
public int getTimesBounced() {
|
||||
return timesBounced;
|
||||
}
|
||||
|
||||
//Bounces the ball, all this does is add
|
||||
//one to the timesBounced instance variable
|
||||
public void bounceBall() {
|
||||
timesBounced++;
|
||||
}
|
||||
|
||||
//A normal toString method, that provides the
|
||||
//instance information about the Ball that we
|
||||
//are currently working with as a String
|
||||
public String toString() {
|
||||
return "This ball is " + color + " with a diameter of " +
|
||||
diameter + " and has been bounced " + timesBounced +
|
||||
|
||||
@@ -1,14 +1,45 @@
|
||||
/*
|
||||
CS7 Challenge Solution
|
||||
|
||||
Original Challenge Description:
|
||||
|
||||
Write a class Ball with the following:
|
||||
Characteristics
|
||||
diameter (double)
|
||||
color (string)
|
||||
timesBounced (int)
|
||||
Behaviors
|
||||
bounceBall
|
||||
getDiameter
|
||||
getColor
|
||||
getTimesBounced
|
||||
Create 2 Balls
|
||||
Ball 1
|
||||
Diameter: 5
|
||||
Color: Red
|
||||
Bounce the ball 10 times
|
||||
Ball 2
|
||||
Diameter: 1
|
||||
Color: Yellow
|
||||
Bounce the ball 1 time
|
||||
Print all the values for both balls
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
//Create two balls, one 5 units in diameter and red,
|
||||
//and another that's one unit in diameter and yellow
|
||||
Ball ball1 = new Ball(5, "Red");
|
||||
Ball ball2 = new Ball(1, "Yellow");
|
||||
|
||||
//Using a for loop, bounce ball1 10 times
|
||||
for(int i = 0; i < 10; i++) {
|
||||
ball1.bounceBall();
|
||||
}
|
||||
|
||||
//Bounce ball2 once
|
||||
ball2.bounceBall();
|
||||
|
||||
//Print information about both ball1 and ball2
|
||||
System.out.println(ball1);
|
||||
System.out.println(ball2);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user