Question

Here is my working code.

import java.util.ArrayList;

public class Cars {

        String Type;
        String Make;
        String Model;
        int Price;      
        int NoDoors;

        public static void main(String [] args){

            ArrayList <Cars> vehicleArray = new ArrayList<Cars>();
            vehicleArray.add(new Cars("Car","Peugot","206",1495,5));
            vehicleArray.add(new Cars("Car","BMW","M3",34495,3));   
            vehicleArray.add(new Cars("Car","FIAT","PUNTO",3495,5));    
            vehicleArray.add(new Cars("Car","FORD","GT",41495,3));

            System.out.println("List of Cars :" + "\n");

            for(Cars car : vehicleArray){
                System.out.println("Type: " + car.getType());
                System.out.println("Make: " + car.getMake());
                System.out.println("Model: " + car.getModel());
                System.out.println("Price: " + car.getPrice());
                System.out.println("No of Doors: " + car.getDoors() + "\n");
            }           

            Cars lowestPrice = vehicleArray.get(0);
            for(Cars car : vehicleArray){
                if(lowestPrice.Price>car.Price){
                    lowestPrice = car;                  
                }
            }
            System.out.println("\n" + "Cheapest Car : ");
            System.out.println("Type: " + lowestPrice.getType());
            System.out.println("Make: " +  lowestPrice.getMake());
            System.out.println("Model: " +  lowestPrice.getModel());
            System.out.println("Price: " + lowestPrice.getPrice());
            System.out.println("No of Doors: " + lowestPrice.getDoors() + "\n");

        }

        public Cars(String type, String make, String model, int price, int Doors){
            Type = type;
            Make = make;
            Model = model;
            Price = price;
            NoDoors = Doors;

        }

        public String getType(){
            return Type;
        }

        public String getMake(){
            return Make;
        }

        public String getModel(){
            return Model;
        }

        public int getPrice(){
            return Price;
        }

        public int getDoors(){
            return NoDoors;
        }
}

I was told that aswell as doing a get method i had to use a set method too. But without the set method my program still works. I am just wondering if the set method is important

Was it helpful?

Solution

You shouldn't just add things because someone tells you to; you should add them when they're a requirement.

As your code is right now, it simply constructs your Cars (keep in mind that an object is typically singular, not plural) object through the constructor. If you never want to be able to change the properties of a car after it has been created then you're fine as it is.

A setter is there to provide manipulating access to the backing field. If you don't need or want this, then you shouldn't add it.

Sidenote: it might still be a good idea to add it but make it private instead. If you want validation logic (for example to check that the price is not negative), then this should be done through a setter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top