Note: in this homework, we will start deducting points
3A
In this problem you will write a class ElectricCar to model an electric car with a specified driving range. The class will need to remember three things:
The way a class remembers data is by declaring and using instance variables. Call the instance variables maxRange, currentRange, and make. Those should be the only three instance variables
The ElectricCar constructor takes the make and the number of miles this car can go when fully charged (maxRange) as a parameter. It sets an instance variables equal to these arguments. It also initializes current range to 0. That is, the car is uncharged when created.
An ElectricCar has one constructor
public ElectricCar(String make, int theMaxRange) - constructs an ElectricCar with the given make and maximum rangeand these methods.
public void charge() - charges the ElectricCar to its maximum range.public void drive( int miles) - drives the car the specified distance. This reduces the current range. public String getMake() - gets the make of the ElectricCarpublic int getCurrentRange() - gets the number of miles the car can go on the current chargepublic int getMaxRange() - gets the maximum number of miles the car can go when fully charged At this point in your programming career, assume the car never tries to drive when there is not enough charge. We will have to cover if statements to be able to handle this properly.
The methods and constructor are provided as stubs in the starter file. A stub has a method header and a body with no implementation. The stub for an accessor method returns 0 for numbers or null for objects like strings. A stub for a mutator method has no body at all. A class with stubs for methods will compile, but you will need to supply implementation and the correct return values. The idea is that you can implement one method at a time and test it since the class will compile. This technique is frequently used in development of applications.
Javadoc is also provided for this class. Study how it is done so you can write the required Javadoc in the next problem
For the draft, declare the three instance variables. Then complete the constructor and getMaxRange methods.
3B
HAL 9000 is a fictional character in Arthur C. Clarke's 2001: A Space Odyssey. HAL (Heuristically programmed ALgorithmic computer) is a AI (artificial intelligence) computer that controls the systems of the spacecraft and interacts with the ship's crew.
In this assignment you will finish the Hal9000 class.
The Hal9000 constructor takes a String parameter of a name of the crew member it is interacting with.
It has methods:
public String getName() which gets the name (Provided)public void setName(String newName) which sets a new name (Provided)public String greet() which returns a string consisting of "Greetings, name" where name is the name supplied in the constructor.public String giveStatus() which returns a string consisting of "Everything is a go, name" where name is the name supplied in the constructor.public String executeCommand(String whatToDo) which returns a string consisting of "I am sorry, name. I can't " + whatToDo where name is the name supplied in the constructor and whatToDo is the parameter.Supply Javadoc for the constructor and all the methods
You can use the String concat method to join strings together, but I find the easiest thing to do is use the + operator. You will learn about that in lesson 4, but I can tell you now. Look at this example of combining two Strings to make a third. I want to combine the phrase "My cat was " with the variable containing my cat's name and then a period.
String cat = "Tiger" String message = "My cat was " + cat +".";
This is called concatenation.
For the draft, write the constructor to initialize (to a assign the value to) the instance variable. The instance variable is defined for you. Do not change it. Provide stubs for all the methods not provided (two are provided for you. You do the other three) and supply all the Javadoc for the constructor and all five methods. The Javadoc for the class itself is provided
What is a stub? A stub has a method header complete with braces. If the method is void, that is all you need. If not, then you need to return a temporary dummy value: null for Strings and other objects, 0 for doubles and integers.
Here is the stub for executeCommand()
public String executeCommand()
{
return null;
}
In the Javadoc, describe what the final version of the method will do, not what the stub does now.
3C
In this problem, you will revisit hw2c. In that homework, you drew the letters COAT on a graphical canvas. Your client wants to use COAT as his logo and to have several COAT's in different places on the Canvas. You could recalculate all the coordinates needed to draw each one, but then what if the client wants to add another or to change the locations? That's a lot of work. Fortunately object-oriented programming provides a solution - Create one class from which you can make multiple objects in different locations.
Make a Bluej project and select Project->Import ... to add the graphics files to your project. Be sure the folder you import contains the files directly and does not contain a second folder that contains the files. (See the screenshot from hw2C) Create two classes Coat and CoatViewer. Replace the code in CoatViewer with the code in Codecheck. You should not modify CoatViewer at all. You will write the Coat class yourself.
CoatViewer is an application with a main method. Right click and execute its main method to see your drawing when your code is finished.
In hw2c, you hard-coded the coordinates of the upper left-hand corner of drawing as (20, 10). That means the drawing is always at the same location. We want to be able to start the drawing at different locations. To do that, we will write a constructor for the class. The constructor will take the x and y coordinates of the upper, left-hand point of the C as parameters.
You will calculate all the other coordinates relative to that x, y. For example,the coordinates of the point on the left-side top of the C are (x,y). And the coordinates of the point on right-side top of the C would then be (x + 30, y) (Remember that 30 is the width of the letter) When you know the left and right coordinates of a line, you can draw it.
Constructor
ublic Coat(int theX, int theY) The constructor initializes the instance variableness. Do not draw in the constructor.Method
public void draw() which will create the drawing as specified below. (The daw method should be able to draw the figure at any location, not just those in the the viewer)Include Javadoc for constructor and method
Your drawing should follow these specifications.
It is a good idea to figure out the coordinates of each letter before starting to draw. I did not follow my own advice. I thought this was simple enough and I was clever enough to figure out the coordinates as I coded. I was so wrong, and it cost me a lot of time.
For the draft, provide instance variables, for x and y. Write the constructor to initialize the instance variables. Add a draw method which will just draw the C. Write the Javadoc for the class, the constructor, and the method.