|
Purpose: The purpose of this assignment is to allow you to practice enumerated types and inheritance. Some topics associated with inheritance are constructors, access rights, method overriding, and so on. You will also practice the creation and use of packages.
Part 1
The description of various moving vehicules is given as follows. A Vehicule class concerns itself with fuel type (String type) such as unleaded, diesel, hybrid, the consumption (double type) which is measured in liter/100km and the price (int type).
A Truck is a Vehicule that additionally concerns itself with the following: type of truck (enumeration type that can be: Flatbed, Box, Refrigerator,Tank).
A Car is a Vehicule that additionally concerns itself with the following: brand of car (String type) such as Toyota, Subaru, Ford etc., make of the car (String type) such as Tercel, Outback, etc. and year(int type).
A FamilyCar is a Car that additionally concerns itself with the following: type of family car (enumeration type that can be : Sedan, Van, SUV) and the maximum number of passengers it can hold(int type).
A SportsCar is a Car that additionally concerns itself with the following: whether it is convertible or not (Boolean type) and the maximum speed it can travel in km/hr (int type).
A Bus is a Vehicule that additionally concerns itself with the following: type of bus (enumeration type that can be: city, school, coach) and the maximum passenger capacity (int type).
You are required to:
1. Draw a UML representation for the above class hierarchy. Your representation must be accurate in terms of UML representation of the different entities and the relation between them. You are asked to use a software to draw the UML . For more information on UML refer to Section 12.1 of your textbook. No hand-writen or hand drawn diagrams accepted.
2. Write the implementation class of the above classes using inheritance and satisfying the following specifications.
a. You must have 4 different Java packages for the classes
A package which includes the Vehicule class.
A package which includes the Car, FamilyCar and SportsCar classes.
A package which includes the Truck class.
A package which includes the Bus class.
b. For each class you must have at least 2 constructors: the default constructor as well as a parametrized constructor which will accept enough parameters to initialize ALL the attributes of the created object. For example the parametrized constructor of FamilyCar class accepts 8 parameters to initialize the consumption, the fuel type, the price, the brand, make and year as well as the maximum number of passengers and the type of family car.
c. An object creation using the default constructor must trigger the default constructor of its ancestor classes. Similarly the parametrized constructor must trigger the parametrized constructors of its ancestors.
d. For each of the classes you must include at least the following methods: accessors, mutators, toString(), equals(). The last 2 methods are always being overrriden.
The toString() method must display clear descriptions and complete information of the object. For example This Family Car is a hybrid, consumes 9.2 liters/100km and costs $25000. It is a 1998 Toyota Camry. It is a sedan which can accommodate 5 passengers.
The equals() method returns true if all attributes of the compared objects are the same values; false otherwise.
e. For all classes other than the Vehicule class you are required to use either private or package access right. For the Vehicule class you are required to use protected access right.
f. When accessing attributes from a base class, you must take full advantage of the permitted rights. For instance, if you can directly access an attribute by name from a base class, then you must do so instead of using a public method from that base class to access the attribute.
3. Write a driver program (where the main() method is) that would utilize all of your classes. The driver class can be in a separate package or in any of the already existing four packages. In the main() method you must:
a. Create various objects from the 6 classes, and display all their information using the toString() method;
b. Test the equality of some to the created objects using the equals() method;
c. Create an array of 10 Vehicule objects and fill that array with various objects from the 6 classes (each class must have at least one entry in that array);
d. Trace that array to find the object that has the cheapest price. Display all information of that object along with its location (index) in the array.
Part 2
In this part you will be modifying your implementation from Part 1 as follows:
1. All classes must have the most restrictive (secure/protective) access rights to their attributes. Adjust your implementation from Part 1 accordingly.
2. Modify the equals() method of the classes so that the method would first check if the passed object (to compare to) is both not null and that it is of the same type of the calling object. The method would clearly return false if any of these conditions is true; otherwise all attributes of the compared objects are the same values.
3. In the driver program, you must add sufficient code to test your additions/changes.
|
|
|
|
|
mastdesi wrote: 1. Draw a UML representation for the above class hierarchy. Your representation must be accurate in terms of UML representation of the different entities and the relation between them. You are asked to use a software to draw the UML . For more information on UML refer to Section 12.1 of your textbook. No hand-writen or hand drawn diagrams accepted.
If you don't have anything already, I'd recommend using Umlet[^]; very easy to use.
Now, instead of posting the whole question, tell us what your problem is.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
CP community will not help you with your college/school assignments. This is something you need to figure out yourself, this is your time to form and strengthen a conceptual understanding of solving a software engineering problem. This is the reason you are taking this course to begin with. If we work on your assignments, then what have you learned? problems and hurdles make you think and read and understand more which is how you build your knowledge. Codeproject isn't and shouldn't be a "one stop shop for your programming assignment needs". If you, regardless of this, want your assignment done by someone else, then may be outsource it to someone in China or India. There are people who will do your assignment for a meagre some of money. Sorry if my statements sound acerbic but that's what you get for not even trying to solve your own problem.
Hope this helps..
Cheers.
|
|
|
|
|
Hillo, I'm Jhencer I have problem can you help me with this....
Our teacher wants us to make a code for multilevel queue.
Can you help me? Please
Multilevel queue is a combination of FCFCS and Round Robbin algorithm..
There is shifting of algorithms that will happen during the process..
the shifting would be 80% for the RoundRobbin aLgo and 20% for FCFS..
Please help me T_T..
|
|
|
|
|
jhencer111108 wrote: Our teacher wants us to make a code for multilevel queue.
That is to say the teacher wants you to do it. But you could take a look here[^] for some useful suggestions.
|
|
|
|
|
I started it for you, when you get stuck come back and tell us:
public class MultiLevelQueue<T> {
public MultiLevelQueue() {
}
public void addItem(T item) {
}
public T getNext() {
}
}
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
Just got home from my wedding trip and only 4 days left at one java exercise before it gotta be delivered in.
I've made one class "Bil1" but when i'm doing the client program
Bil1 minBil = new Bil1() the compiler gives me one error.
C:\Java\Øving 5>javac bil.java
bil.java:43: cannot find symbol
symbol : constructor Bil1()
location: class Bil1
Bil1 minBil = new Bil1();
^
1 error
Got no idea where I've done wrong here.
here is the complete code also.
class Bil1{
public final String regNr;
public final String merke;
public final int årsmodell;
public final int hastighet;
public final boolean motorenIGang;
public Bil1(String regNr, String merke, int årsmodell, int hastighet, boolean motorenIGang){
this.regNr = regNr;
this.merke = merke;
this.årsmodell = årsmodell;
this.hastighet = hastighet;
this.motorenIGang = motorenIGang;
}
public String getRegNr() {
return "VD-12345";
}
public String getMerke() {
return "Volvo";
}
public int getÅrsmodell() {
return 2002;
}
public int getHastighet() {
return 55;
}
public boolean getMotorenIGang() {
return true;
}
}
class Bil1Test {
public static void main(String[] args) {
Bil1 minBil = new Bil1();
System.out.println("Regnr " + minBil.regNr
+ ", merke " + minBil.merke
+ ", årsmodell " + minBil.årsmodell
+ ", hastighet " + minBil.hastighet
+ ", motorenIGang " + minBil.motorenIGang);
}
}
|
|
|
|
|
The Java default constructor is only available if no other constructors are provided.
If you provide a constructor that takes parameters you lose the default constructor and therefore must explicitly define it.
See the last paragraph before the 'overloads' section; http://www.javabeginner.com/learn-java/java-constructors[^]
|
|
|
|
|
Been playing around and changing the Bil1 class return values but nothing have helped.
I did try
Bil1 minBil = new Bil1("VD-12345", "Volvo", 2002, 55, true);
and that worked like an charm.
but then the client program ain't approved. the task is to have the values in the class section "Bil1", not client section "Bil1test"
modified 4-Oct-11 18:49pm.
|
|
|
|
|
Tor Danielsen wrote: Bil1 minBil = new Bil1("VD-12345", "Volvo", 2002, 55, true);
I did put something like that in my initial message (that i then deleted, because I started thinking down the default constructor route).
Tor Danielsen wrote: but then the client program ain't approved. the task is to have the values in the class section "Bil1", not client section "Bil1test"
I haven't got a clue what you mean by that.
I am completely confused what you are exactly wanting now. Basically, things wont work if your method signatures don't match, so you either need to remove the paramaters or create a new empty constructor in the the Class (for the reasons I posted in the the message above).
|
|
|
|
|
What Dave said (in his first reply) is correct and very relevant.
What would be the point of having a class Bil1 with all those variables inside it, if all it does is return some constants??? All Bil1 instances would be equivalent and behave identically.
modified 4-Oct-11 19:54pm.
|
|
|
|
|
A slightly modified version of your program:
public class Bil1{
private final String regNr;
private final String merke;
private final int årsmodell;
private final int hastighet;
private final boolean motorenIGang;
public Bil1() {
this (null, null, 0, 0 false);
}
public Bil1(String regNr, String merke, int årsmodell, int hastighet, boolean motorenIGang){
this.regNr = regNr;
this.merke = merke;
this.årsmodell = årsmodell;
this.hastighet = hastighet;
this.motorenIGang = motorenIGang;
}
public String getRegNr() {
if (this.regNr == null) {
return "Ukjent";
}
return this.regNr;
}
public String getMerke() {
if (this.merke == null) {
return "Ukjent";
}
return this.merke;
}
public int getÅrsmodell() {
return this.årsmodell;
}
public int getHastighet() {
return this.hastighet;
}
public boolean getMotorenIGang() {
return this.motorenIGang;
}
public String toString() {
return "Regnr " + this.getRegNr()
+ ", merke " + this.getMerke()
+ ", årsmodell " + this.getÅrsmodell()
+ ", hastighet " + this.getHastighet()
+ ", motorenIGang " + this.getMotorenIGang()
}
public int hashCode() {
return 7 * ((this.getRegNr().hashCode() +
9 * this.getMerke()) +
3 * this.getÅrsmodell()) +
this.getHastighet() +
(this.getMotorenIGang() ? 1 : 0);
}
public boolean equals(Object obj) {
if (obj == null || obj.getClass() != this.getClass()) {
return null();
}
Bil1 that = (Bil1)obj;
return this.getRegNr().equals(that.getRegNr()) &&
this.getMerke().equals(that.getMerke()) &&
this.getÅrsmodell() == that.getÅrsmodell() &&
this.getHastighet() == that.getHastighet() &&
this.getMotorenIGang() == that.getMotorenIGang();
}
}
public class Bil1Test {
public static void main(String[] args) {
Bil1 minBil = new Bil1("VD-12345", "Volvo", 2002, 55, true);
System.out.println(minBill);
}
}
Now, the lessons are:
- Member variables should be private. Just don't go any other way without very good reason, you don't have one.
- Try to have all constructors lead to a single one with all - or most - of the logic. If you wish to change the way it is initialising the class, then only one place needs top be changed.
- Internally use methods rather than variables. It is possible that you need to change the internal representation of a member variable but not the method. This ensures that the methods always get what is expected - design by contract - as you'll see from the way I've changed the methods for make and model.
- If you need to describe an object, let the object do it. Every class should override toString , equals , and hashCode .
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
Your code makes no sense; you have a constructor that takes 5 values and stores them within your object, but your property getters all return static values. So any object of the class will always be a 2002 model Volvo with reg number VD-12345. You should change your getters to return the values stored by the constructor. You should also not try to instantiate an object using a constructor (the no parameter version) that does not exist in your class. You can find more details about classes, objects and constructors here[^].
|
|
|
|
|
PLEASE
Use a IDE for your development. Eclipse and Netbeans are FREE - there is no reason to not use them.
And if you'd use them you would have seen your failures right away.
regards Torsten
I never finish anyth...
|
|
|
|
|
Often 'skool kidz' are encouraged not to use IDE's as it can actually impair their learning the language. There is one, BlueJ[^], that is specifically designed for learning Java. It provides good build tools but takes away 'cheats' like auto-suggestion and intellisense. It has added features such as being able to create objects on the fly, great for testing methods. It is a very good learning aid and I would seriously recommend this for students over NetBeans or Eclipse so they can learn the language; rather than the IDE.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
I started my Java programming using vi and progressed to emacs, and used javac to compile. There was nothing wrong with doing it that way, and it was just as easy to find and fix syntax and other errors. But I agree, an IDE is much easier.
|
|
|
|
|
you started with vi and emacs because there was no IDE at that time. That's a difference.
regards Torsten
I never finish anyth...
|
|
|
|
|
Very true, but it's still possible to learn Java (and indeed any other language) without an IDE.
|
|
|
|
|
I started Java programming using Notepad for coding and the command line to compile things. I'm glad I did because I learned a lot about the language that way, looking up Java In A Nutshell to find out what method to use. I still subscribe to the view that if you are new to the language, using an IDE like Eclipse or NetBeans can hinder your understanding of things. Swing programming is a good example: IDEs allow you to draw the layout you want visually, and then it generates the code. Faster and less error-prone, but if that's all you ever do it will take you longer to learn the nuts and bolts of how Swing actually works.
On the other hand, I have never heard anyone heard anyone suggest people should learn C# that way. The idea that people should use Notepad to learn C# before starting with Visual Studio wouldn't really be taken seriously by anyone. I'm not really sure what that says about learning Java as opposed to learning C#.
|
|
|
|
|
It doesn't matter if a WYSIWYG-Editor is included - you need to know how to use such something too. It just positions the GUI-components, that's all. You still need to know for example what a GridLayout is.
Also do learners often not even use a GUI - they just push some command lines around.
regards Torsten
I never finish anyth...
|
|
|
|
|
TorstenH. wrote: You still need to know for example what a GridLayout is.
Well, interestingly NetBean's visual designer (Matisse) is based around a layout called GroupLayout which is not really intended to be hand-coded. The idea is to get away from requiring the developer to know all the ins and outs of the layout and make it easier to design UIs visually.
One way to look at it is a move towards the model of something like C#, where developers don't need to know how the GUI is constructed, just how to wire the components together and respond to events.
|
|
|
|
|
David Skelly wrote: One way to look at it is a move towards the model of something like C#VB, where developers don't need to know how the GUI is constructed, just how to wire the components together and respond to events.
Yea, that worked really well didn't it...
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
use the default constructor as
pubilc bill()
{
}
inside the class bill
|
|
|
|
|
Hi all,
I need a sample working CODE OR PROGRAM for this java - eclipse ,
Search Service - customization in oracle Customer Care and Billing .
|
|
|
|
|
Start here[^]
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|