Notice: Assignments are currently being rewritten for CSAlpha. This assignment was created for an old version of the curriculum and may no longer align with the current curriculum.
If you have any questions regarding the assignment or see any errors in the assignment writeup, please let me know.
Create a text file called assign4.txt
. You will be using this to respond to questions in the assignment writeup and paste your code.
There are many types of cars. However, all these types of cars have some common characteristics: they have four wheels, we can drive them, and though their colors might be different, they all have a color scheme. Without these common characteristics, we can't tell the difference between a car and another object. These traits are imperative for the ability to classify a particular object and declare what type of object it is.
A class is essentially just that; it is a declaration of type. Particularly, it is the declaration of type through defining the shared attributes of objects with that type. Below is the beginning of the definition of a class named Car:
xxxxxxxxxx
public class Car {
}
We create the Car class so that if we happen to create multiple cars in our program, we already have the blueprint to make that object. We don't want to keep redeclaring the shared characteristics of cars every time we want to make one; since we already know that these are common attributes, we can declare them once and reuse them by using the same Car class.
Java is an Object Oriented Programming Language. It emphasizes the use of classes and objects to encourage reusable, modular code. When you program a class with OOP in mind, you hide away all the implementation details of an object and use it a higher level. For example, a person uses a car to drive, but isn't worried about how the car works internally. This concept is known as abstraction, and we'll learn more of these OOP concepts as we move along in the course.
Returning to our Car class, we have to include what's called a constructor. This is the function that is called every time we want to make a new Car. Its syntax is defined below:
xxxxxxxxxx
public class Car {
Car() {
//some code to run when a new car is created
}
}
Now, as said above, we need to declare some common characteristics that all cars have. We can do this by declaring properties local to the class, like below:
xxxxxxxxxx
public class Car {
String color;
int maxSpeed;
Car() {
//some code to run when a new car is created
}
}
Note that we haven't said anything about what the values of the properties actually are. Referring back to our color example earlier, the color for different cars is different. But since cars all have color, we include it as a local property to say that cars have color, but they don't all have the same color.
Because of this, we might want to define color
and maxSpeed
when we create a car. Where can we do this? Hint: look at the orange comment.
The constructor! When we create the object, we want to pass in the color
and maxSpeed
, so we'll have to add parameters to the constructor. Then, inside the constructor, we want to pass in the parameters to the local properties that belong to the Car:
xxxxxxxxxx
public class Car {
String color;
int maxSpeed;
Car(String color, int maxSpeed) {
this.color = color;
this.maxSpeed = maxSpeed;
}
}
The reason we use the keyword this
is because our parameters have the same name as our class local properties. Thus, we need to have a way to refer to the properties that belong to the class and not the parameters of the constructor, which is exactly what this
does.
Finally, objects have uses and actions. A car is meant to be driven; let's add a drive()
method for our Car class. I'll also add a speed
local property to keep track of how fast the car is going:
xxxxxxxxxx
public class Car {
String color;
int maxSpeed;
int speed = 0;
Car(String color, int maxSpeed) {
this.color = color;
this.maxSpeed = maxSpeed;
}
void drive() {
speed++;
if (speed >= maxSpeed) {
speed = maxSpeed;
}
}
}
What speed do newly created cars start out at? Can you explain what the drive()
function does in plain English? Put your answers into your assign4.txt
.
Objects are the things that are created from the blueprints of classes. With our class above, we can create a Car object by calling to the constructor we made. To access the car's local properties and methods, we use the dot operator. Take a look at the example below:
xxxxxxxxxx
public static void main(String[] args) {
Car redCar = new Car("red", 50);
System.out.println(redCar.color);
for (int i = 0; i < 100; i++) {
redCar.drive();
}
System.out.println(redCar.speed);
}
What is the output of the above program? Write your answer in assign4.txt
.
We can also create multiple cars that have all their own, separate properties:
xxxxxxxxxx
public static void main(String[] args) {
Car redCar = new Car("red", 30);
Car blueCar = new Car("blue", 50);
blueCar.drive();
System.out.println(redCar.color);
System.out.println(blueCar.color);
System.out.println(redCar.speed);
System.out.println(blueCar.speed);
}
Program output:
red
blue
0
1
How useful!
Someone has hired you to create a to-do list Java app for them. In terms of OOP, you already know what you'll begin with: a class that will be the blueprint for each task object in the to-do list. You'll call the class Task
.
Upon telling the client this, they tell you that they'll send the implementation details for what a Task
should be able to do. A week later, they send an email with all the implementation details, listed below:
Tasks should have two attributes that are set upon task creation: a name and description.
I want a task to have two possible states: unfinished and finished. When a task is created, it should always start with the unfinished state.
Not only that, I want you to be able to keep track of how much progress a task is currently at, from a range of 0% to 100%. For example, the task could be at 57% out of 100% progress. The progress should start out at 0% for a new task.
I think there should be three actions that you can have with a task regarding progress:
There should be the ability to increase or decrease the progress of a task by an arbitrary amount requested by the user, but I'm a little worried that the progress might go below 0% or above 100% with this feature. Just be safe about it and add some prevention technique to block that from happening.
The other action should be the ability the print out the current progress in this format: "(task name) Progress: (progress)% out of 100%". For example, a task could print out something like this: "Group Project Progress: 50% out of 100%".
Once you are done with the job, test out to see if it works in the Main
class. Then please paste it in assign4.txt
.
-Client
After you've created the Task
class, you are not quite sure the client will be satisfied with testing happening in the Main
class exclusively. So you've decided to design a testing harness that doesn't require any coding to use. To the console!
Your objective with this testing harness is to design an interactive prompt in the console that creates and interacts with a Task
based on user input. Here's an example of what this would look like:
xxxxxxxxxx
Welcome to task creation. Please enter a name:
>Group Project
Please enter a description:
>Class Programming
Group Project was created.
Enter a command:
>increase progress
Group Project: 1% out of 100%
Enter a command:
>view progress
Group Project: 1% out of 100%
Enter a command:
>decrease progress
Group Project: 0% out of 100%
Enter a command:
>view details
Group Project: Class Programming
Enter a command:
>end
Program Terminated
The shell prompt character (>
) indicates user input, but you are not required to include it in the test harness. The test harness should run until the command end
.
Here is a list of all the commands you are required to implement:
Paste your work in assign4.txt
.