$25
The point of this assignment is to cover a few of the basic principles of classes and objects including the proper use of methods.
The solution for this assignment (at least the one I implemented) can be accomplished by inserting code to determine if a prime number ends in the digit entered by the user.
Using the IDE
.
Initial Code & Output
Load the following code into the online compiler:
Please copy the following code into the IDE, compile and run it.
import java.util.Scanner;
class Car {
public void start () {
System.out.println("The Car Has Started\n");
}
}
public class MyClass {
public static void main(String args[]) {
System.out.println("A06 - Written by Matt Weisfeld\n");
String consoleInput = null;
Scanner console = new Scanner(System.in);
Car myCar = new Car();
System.out.print("Please enter a color (or 'x' to end): \n");
consoleInput = console.nextLine();
System.out.println("\nYou entered " + consoleInput + "\n");
myCar.start();
System.out.print("Goodbye!");
}
}
This code is an introduction to creating objects and passing arguments to those objects. We will cover much of this in detail later in the course; however, take this opportunity to study the code and determine what is going on.
When you execute the code it will look something like this:
Problem
Add the constructor for the Car class and add the accessor (getters and setters) methods for the color attribute.
https://www.tutorialspoint.com/java/java_constructors.htm
https://www.w3schools.com/java/java_encapsulation.asp
Here is an article I wrote many years ago on encapsulation which is what accessor methods support.
https://www.developer.com/java/other/article.php/10936_3374921_3/Exploring-Encapsulation.htm
Here are 5 constraints that you must include in your program.
1) Include an output statement at the beginning of the program with the assignment number and your name:
A06 – Written by Matt Weisfeld
2) The constructor must pass a single argument to initialize the color.
3) When the constructor is called, set the car’s initial color to Green.
4) Include a loop to change the color until an “x” is entered.
5) Include a condition to test for the “x” and when found use a break to exit the loop.
6) Print “goodbye” upon exiting the program.
Final Output
Once completed, your output (in the following test case) should look like this:
Test case: enter Red, Blue, x
Note the input box