Design a Class for a Car: Attributes and Methods

  • Thread starter Thread starter courtrigrad
  • Start date Start date
  • Tags Tags
    Design
Click For Summary
SUMMARY

The discussion focuses on designing a class for a car in object-oriented programming (OOP), specifically addressing the attributes and methods required. Key attributes include make (e.g., Toyota, Ford), mileage, total gallons used, fuel efficiency classification, miles per gallon, and vehicle weight. The user is advised to implement accessor methods for all attributes and setter methods only for the make, mileage, total gallons used, and vehicle weight. Additionally, the user is reminded to correctly format method headers, ensuring parentheses are included.

PREREQUISITES
  • Understanding of object-oriented programming concepts
  • Familiarity with class design and instance variables
  • Knowledge of accessor and mutator methods
  • Basic syntax of Java programming language
NEXT STEPS
  • Review Java class design principles and best practices
  • Learn about accessor and mutator methods in Java
  • Study examples of class usage in OOP
  • Practice designing classes with multiple attributes and methods
USEFUL FOR

Students preparing for exams in object-oriented programming, software developers learning class design, and anyone interested in improving their understanding of Java programming concepts.

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.
 

Similar threads

Replies
2
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K