Results 1 to 7 of 7
Thread: Need help for Assignment!!!
- 11-20-2011, 05:37 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
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 07:10 PM. Reason: code tags added
- 11-20-2011, 07:06 PM #2
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).
Get in the habit of using standard Java naming conventions!
- 11-21-2011, 02:17 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
Re: Need help for Assignment!!!
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().
- 11-21-2011, 02:38 AM #4
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
Re: Need help for Assignment!!!
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.
-
Re: Need help for Assignment!!!
Moderator action: RichersooN post deleted. I hope you were joking, right?
-
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.
- 11-23-2011, 06:37 PM #7
Re: Need help for Assignment!!!
Okay, let's look at (b) first:
You've done this:(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.
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.Java Code:HashMap<Car, Double> carMap = new HashMap<Car, Double>();
Get in the habit of using standard Java naming conventions!
Similar Threads
-
Can somebody help me in this assignment?
By lulzim in forum New To JavaReplies: 25Last Post: 03-01-2011, 11:02 PM -
Need Help With Assignment
By zeo in forum New To JavaReplies: 10Last Post: 02-25-2011, 12:09 AM -
Need help with a HW assignment
By mackavelirip in forum New To JavaReplies: 4Last Post: 02-17-2011, 01:36 AM -
Need help with assignment! please
By runawaykinms in forum Java AppletsReplies: 2Last Post: 10-06-2010, 09:58 AM -
I am looking for help with an assignment
By nanoo51969 in forum New To JavaReplies: 1Last Post: 03-23-2009, 09:41 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks