Design a Class for a Car: Attributes and Methods

  • Thread starter courtrigrad
  • Start date
  • Tags
    Design
In summary: You can also refer to the class CarDemo in the textbook. In summary, the conversation discusses an upcoming exam on Program Design and OOP Concepts, with a focus on distinguishing between terms and a more challenging design question. The task is to design a class representing a car with specified instance variables and methods for accessing and modifying them. The code provided by one of the participants has some errors and the conversation ends with a request for assistance and clarification on how to call the methods.
  • #1
courtrigrad
1,236
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
  • #2
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.
 

Related to Design a Class for a Car: Attributes and Methods

1. What attributes are important to consider when designing a car class?

The attributes that are important to consider when designing a car class include the make and model of the car, the year it was manufactured, the color, the number of doors, the engine type and size, the transmission type, the fuel type, and the size and type of tires.

2. What methods should be included in a car class?

The methods that should be included in a car class depend on the specific needs and functionalities of the program. However, some common methods that can be included are start/stop engine, accelerate/brake, change gears, turn on/off lights, and open/close doors.

3. How can the car class handle different types of engines?

The car class can handle different types of engines by having an attribute for the engine type, such as "gasoline" or "electric", and using conditional statements in the methods to perform different actions based on the engine type. The class can also have different sub-classes for each type of engine, with specific methods and attributes for each.

4. Can the car class have methods for maintenance and repairs?

Yes, the car class can have methods for maintenance and repairs. These methods can include checking and changing the oil, replacing tires, and performing diagnostic tests. The class can also keep track of the car's mileage and notify the user when it is time for regular maintenance.

5. How can the car class handle different types of transmissions?

Similar to handling different types of engines, the car class can have an attribute for the transmission type, such as "manual" or "automatic", and use conditional statements in the methods to perform different actions. The class can also have separate sub-classes for each type of transmission, with specific methods and attributes for each.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top