Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By kjkrum

Thread: Need help for Assignment!!!

  1. #1
    HolyPK is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Need help for Assignment!!!

    Please help to check with Part a and help for Part b
    I have no idea what should i do with Part b... Million Thanks


    (a) Create a class Car which stores the information of a car. It includes the brand, the
    model number (String) and the price (double) . Write a constructor of the class to so that the
    information mentioned are initialized when a Car object is created. Also write the getter methods
    for those variables. Finally add a method toString() to return the car information in the
    following string form.
    "Brand: Benz, Model no.: M123, Price: 30000.0"

    Please help to Double Check!!

    Java Code:
    class Car{
        String brand, modelNumber;
        double price;
    
        private Car(String cBrand, String cModelNumber, double cPrice) {
            this.brand = cBrand;
            this.modelNumber = cModelNumber;
            this.price = cPrice;        
        }
     
        void setBrand(String newBrand) {
            this.brand = newBrand;
        }
        String getBrand() {
            return this.brand;
        }
        void setModelNumber(String newModelNumber) {
            this.modelNumber = newModelNumber;
        }
        String getModelNumber() {
            return this.modelNumber;
        }
        void setPrice(double newPrice) {
            this.price = newPrice;
        }
        double getPrice() {
            return this.price;
        }
        
        public String toString() {
            return "\"Brand: " + brand + "," + "Model no.: " + "," + modelNumber + "Price: " + price + "\"";
        }
    }

    (b) Create a class CarShop which stores the car information in a map carMap,
    whose key is the concatenation of the brand and model number, separated by ": " (a colon and a
    space). Write a method addCar(Car oneCar) which adds oneCar to carMap.
    Last edited by Fubarable; 11-20-2011 at 08:10 PM. Reason: code tags added

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Need help for Assignment!!!

    It's hard to help you without a specific question. Part b seems pretty self-explanatory, except for one thing: what a map is. Check out Map (Java Platform SE 6).
    Fubarable likes this.
    Get in the habit of using standard Java naming conventions!

  3. #3
    HolyPK is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: Need help for Assignment!!!

    Quote Originally Posted by kjkrum View Post
    It's hard to help you without a specific question. Part b seems pretty self-explanatory, except for one thing: what a map is. Check out Map (Java Platform SE 6).
    The whole question is, and i dont understand how to add a method into a hashmap.

    (a) Create a class Car which stores the information of a car. It includes the brand, the
    model number (String) and the price (double) . Write a constructor of the class to so that the
    information mentioned are initialized when a Car object is created. Also write the getter methods
    for those variables. Finally add a method toString() to return the tablet computer information in the
    following string form.
    "Brand: BENZ, Model no.: M123, Price: 30000.0"

    (b) Create a class CarShop which stores the car information in a map carMap,
    whose key is the concatenation of the brand and model number, separated by ": " (a colon and a
    space). Write a method addCar(Car oneCar) which adds oneCar to carMap.

    (c) Create a class TestCarShop with a main() method which creates a CarShop object
    aShop and add the first car with brand "BENZ", model number "M123" and price
    30000. Add the second car with brand "BMW", model number "B332" and price
    25000.

    (d) Write a method showTablet() of CarShop which loops through the keys of carMap using
    the enhanced for-loop and directly prints each car object stored using
    System.out.println(). (Loop through the values is simpler but using the keys is required in this
    part.) This should show suitable information since the method toString() has been written in (a).
    Add a statement in TestCarShop to display all the car information of aShop.

    (e) Write a method priceSet() of CarShop which returns the prices of the car in a
    set. You should loop through the values of carMap using the enhanced for-loop and collect the
    prices. Add a statement in TestCarShop to display the set using System.out.println().

    (f) Write a method brandList() of CarShop which returns the brands of the car in a
    list. You should loop through the values of carMap using the enhanced for-loop and collect the
    brands of the tablet computers. Add a statement in TestCarShop to display the list using
    System.out.println().

  4. #4
    HolyPK is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: Need help for Assignment!!!

    Quote Originally Posted by kjkrum View Post
    It's hard to help you without a specific question. Part b seems pretty self-explanatory, except for one thing: what a map is. Check out Map (Java Platform SE 6).
    import java.util.*;
    public class CarShop {
    HashMap<Car, Double> carMap = new HashMap<Car, Double>();

    public void addCar (Car oneCar) {

    String message = oneCar.getBrand()+": "+oneCar.getModelNumber();
    tabletMap.put(message, oneCar);
    Set<Car> keys = carMap.keySet();
    Collection<Double> values = carMap.values();
    }
    }


    whats wrong with above.... i really dont understand how to add a method into a Map.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help for Assignment!!!

    You're not "adding a method into a Map". You're creating a class that holds a HashMap and then using that Map in the class. Again, read the tutorials on how to use maps, give it a try, and if you get stuck, tell us specifically where you're stuck.

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Need help for Assignment!!!

    Okay, let's look at (b) first:

    (b) Create a class CarShop which stores the car information in a map carMap,
    whose key is the concatenation of the brand and model number, separated by ": " (a colon and a
    space). Write a method addCar(Car oneCar) which adds oneCar to carMap.
    You've done this:

    Java Code:
    HashMap<Car, Double> carMap = new HashMap<Car, Double>();
    Written this way, you're using Cars as keys. The assignment says to use Strings as keys and Cars as values. Keys in a map allow you to look up their values. You want to be able to look up a String like "BMW: B332" and get the associated Car object. The next step is to add a method to your CarShop class that takes a Car, figures out what its key should be, and then stores the key and value in the Map.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Can somebody help me in this assignment?
    By lulzim in forum New To Java
    Replies: 25
    Last Post: 03-02-2011, 12:02 AM
  2. Need Help With Assignment
    By zeo in forum New To Java
    Replies: 10
    Last Post: 02-25-2011, 01:09 AM
  3. Need help with a HW assignment
    By mackavelirip in forum New To Java
    Replies: 4
    Last Post: 02-17-2011, 02:36 AM
  4. Need help with assignment! please
    By runawaykinms in forum Java Applets
    Replies: 2
    Last Post: 10-06-2010, 10:58 AM
  5. I am looking for help with an assignment
    By nanoo51969 in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 10:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •