Design a Class for a Car: Attributes and Methods

  • Thread starter Thread starter courtrigrad
  • Start date Start date
  • Tags Tags
    Design
AI Thread Summary
The discussion revolves around designing a class to represent a car for an exam on Program Design and Object-Oriented Programming (OOP) Concepts. The class should include instance variables for the car's make, mileage, total gallons used, fuel efficiency classification, miles per gallon, and vehicle weight. Clients should have access to all attributes and the ability to modify specific variables (make, mileage, total gallons used, and vehicle weight). The initial code provided contains errors, including duplicate properties and incorrect method headers lacking parentheses. It is clarified that accessor methods (getters) are needed for all properties, while setter methods (modifiers) are only required for the specified modifiable attributes. Additionally, examples of how to call these methods can be found in textbooks or class notes.
courtrigrad
Messages
1,236
Reaction score
2
Hello all

I have an exam tomorrow on Program Design and OOP Concepts. The majority of the exam will consist of questions that test your ability in distinguishing between terms. This will be the easy part. However, the more difficult portion of the test will contain a free response problem of multiple parts called a "design question."

Can you verify if I am doing this correctly:

Design a class to represent CAR. Instance variables stored about a car should be:

1. the make(Toyota, Ford)
2. mileage
3. the total gallons used
4. whether the car is an economy model, a moderately fuel efficient car or a gas guzzler.
5. miles per gallon
6. vehicle weight

Clients of this class should be able to access to find out the value of all attributes about a car. Clients should be able to modify instance variables 1, 2, 3, and 6. In writing the design, do not write any code. Just declare the instance variables and write the header for the methods.

My code:
Code:
public class Car {

private string make;
private double mileage;
private double totalgal;
private string classcar;
private double milespergal;
private double vehicleweight;
private double milespergallon;

public string getMake {
.
.
.

public double mileage {

.
.
.

I am not sure if I am doing this correctly. Do I put accessors and modifiers for each of the methods? How would I call the methods?

Any help is greatly appreciated

Thanks!
 
Last edited:
Computer science news on Phys.org
courtrigrad said:
Hello all

I have an exam tomorrow on Program Design and OOP Concepts. The majority of the exam will consist of questions that test your ability in distinguishing between terms. This will be the easy part. However, the more difficult portion of the test will contain a free response problem of multiple parts called a "design question."

Can you verify if I am doing this correctly:

Design a class to represent CAR. Instance variables stored about a car should be:

1. the make(Toyota, Ford)
2. mileage
3. the total gallons used
4. whether the car is an economy model, a moderately fuel efficient car or a gas guzzler.
5. miles per gallon
6. vehicle weight

Clients of this class should be able to access to find out the value of all attributes about a car. Clients should be able to modify instance variables 1, 2, 3, and 6. In writing the design, do not write any code. Just declare the instance variables and write the header for the methods.

My code:
Code:
public class Car {

private string make;
private double mileage;
private double totalgal;
private string classcar;
private double milespergal;
private double vehicleweight;
private double milespergallon;

public string getMake {
.
.
.

public double mileage {
.
.
.

I am not sure if I am doing this correctly.
There are several problems with your code. You have more properties than the problem asks for (you have a miles-per-gallon property twice). Also, your headers for the accessor methods are not correct -- they are missing the parentheses for the argument list.
courtrigrad said:
Do I put accessors and modifiers for each of the methods?
According to the problem description, you need get accessors for each of the six properties, but you need set accessors only for properties 1, 2, 3, and 6.
courtrigrad said:
How would I call the methods?
Your textbook or notes should have examples of how a client of the class would use the methods on the class.
 
Thread 'ChatGPT Examples, Good and Bad'
I've been experimenting with ChatGPT. Some results are good, some very very bad. I think examples can help expose the properties of this AI. Maybe you can post some of your favorite examples and tell us what they reveal about the properties of this AI. (I had problems with copy/paste of text and formatting, so I'm posting my examples as screen shots. That is a promising start. :smile: But then I provided values V=1, R1=1, R2=2, R3=3 and asked for the value of I. At first, it said...
Sorry if 'Profile Badge' is not the correct term. I have an MS 365 subscription and I've noticed on my Word documents the small circle with my initials in it is sometimes different in colour document to document (it's the circle at the top right of the doc, that, when you hover over it it tells you you're signed in; if you click on it you get a bit more info). Last night I had four docs with a red circle, one with blue. When I closed the blue and opened it again it was red. Today I have 3...

Similar threads

Back
Top