|
equals() does compare the objects and returns true if so.
You've done it right in the class Vehicule. You just need to compare also the rest of the fields:
EDIT: I added super.equals(object) to get also the fields of the extended object compared.
public boolean equals(Vehicule e) {
boolean ans;
if(true == super.equals(e) &&
this.fuelT == e.fuelT &&
this.consum == e.consum &&
this.price == e.price ){
return true;
}
return ans;
}
You should implement it in a similar way into the other classes.
Also are accessors and mutators missing as they are demanded. Would be nice if you use them in the equals() methods.
AND: Vehicle is written without U - but that's just a style thingy.
regards Torsten
I never finish anyth...
|
|
|
|
|
TorstenH. wrote: this.fuelT == e.fuelT
Fail. Stay after class and write out 100 times, "I will not compare Strings by reference".
|
|
|
|
|
*args* I knew it wouldn't be a good day - but that bad?
regards Torsten
I never finish anyth...
|
|
|
|
|
how can i use the accessors and mutators. I think i am wrong with this but can you please check.
public String getfuel()
{
return this.fuelT;
}
public double getconsum()
{
return this.consum;
}
public int getprice()
{
return this.price;
}
public String setfuel()
{
return this.fuelT;
}
|
|
|
|
|
The accessor method returns the member variable value and mutator [never heard it called that] sets the value.
As a guide the structure is always the same and they [usually] exist in pairs. If a variable is immutable then it must be initialised via the constructor and no setter [mutator] method exists.
class Foo {
private Bar bar;
public Bar getBar() {
return this.bar;
}
public void setBar(Bar nar) {
this.bar = bar;
}
}
You will need something like:
public String getFuelType() {
return this.fuelType;
}
public double getConsumption() {
return this.consumption;
}
public int getPrice() {
return this.price;
}
public void setFuelType(String fuelType) {
this.fuelType = fuelType;
}
public void setConsumption(double consumption) {
this.consumption = consumption;
}
public void setPrice(int price) {
this.price = price;
}
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 really don't get the point on how this will help us in the equals. Sorry i am really lost and panicking so can't even think right since this assignment is due in 12 hours from now and i am hardly done.
|
|
|
|
|
The getters aren't there for the other methods but are there for external classes.
I would encourage the use of getters and setters over direct member variable access as it is a safer approach in most cases.
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
|
|
|
|
|
can yo give me a little part of the equals using the accessor and mutator., i don't understand please .
|
|
|
|
|
There is a simple style for equals you should learn, for Vehicule it should be:
public boolean equals(Object obj) {
if (obj == null || this.getClass() != obj.getClass()) {
return false;
}
final Vehicule other = (Vehicule) obj;
return this.fuelT.equals(other.fuelT) &&
this.consum == other.consum &&
this.price == other.price;
}
If you override equals , you should always override hashcode , use the same attributes as are compared in equals :
public int hashCode() {
int hash = 7;
hash = 47 * hash + (this.fuelT != null ? fuelT.hashcode() : 0);
hash = 47 * hash + (Double)this.consum.hashCode();
hash = 47 * hash + this.price;
return hash;
}
The methods must match so that if they are equal, they should have the same hash code, but the same hash code does not mean two objects are equal.
Similarly if you need to implement Comparable, then the compareTo method must match equals so that if two objects are equal then compareTo returns 0 and if comapreTo is non-zero then the objects are not equal.
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
|
|
|
|
|
we haven't learned hash yet.
|
|
|
|
|
Equals and hashCode should be taught together.
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 really need help with my assignment for java. Please give me a little push start with this assignment. I am really bad with getting the concept of the arrays. Please give me the starting part for a couple of classes and the driver class. Once i get started i am sure i can figure it out.
Please check this assignment file and I would appreciate whatever help you guys give me.
Assignment pdf file can be downloaded here : http://www.2shared.com/document/pKqvuo_K/A2help.html
This is the assignment explanation in screen shots of the pdf:
http://imageshack.us/photo/my-images/525/screenshot20111006at112.png/
http://imageshack.us/photo/my-images/13/screenshot20111006at112.png/
http://imageshack.us/photo/my-images/441/screenshot20111006at112.png/
I tried to start this but i am not sure if i need to use arrays in the classes and how to use them.
Vehicule class:
package Package1;
public class Vehicule {
String fuelT;
double consum;
int price;
public Vehicule ()
{
fuelT = "";
consum = 0.0;
price = 0;
}
public Vehicule(String f, double c, int p)
{
this.fuelT = f;
this.consum = c;
this.price = p;
}
}
Car class:
package Package2;
import Package1.Vehicule;
public class Car extends Vehicule {
private String carBrand;
private String carMake;
private int year;
public Car()
{
carBrand = "";
carMake = "";
year = 0;
}
public Car(String b, String m, int y, String f, double c, int p)
{
this.carBrand = b;
this.carMake = m;
this.year = y;
}
}
modified 6-Oct-11 11:29am.
|
|
|
|
|
First off, I'm not going to download an .exe to read your assignment, I don't think many others will either.
Secondly, you haven't saud what the problem is you are having.
Thirdly, a piece of style advice.
When you overload a method, try to make sure that the repeated functions are all in one place. For your constructors in Vehicule:
public Vehicule () {
this("", 0.0, 0);
}
Simples!
Then in Car:
public Car() {
this("","", 0, "", 0.0, 0);
}
public Car(String b, String m, int y, String f, double c, int p)
{
super(f, c, p);
this.carBrand = b;
this.carMake = m;
this.year = y;
}
Now, if you need to change something in the real constructors, you don't need to faff around in four places.
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
|
|
|
|
|
the file is a pdf. sorry about the posting everywhere. its due tomorrow and i am off today so i can work on it. but i can't start off. the teacher says its like the last assignment with arrays used. Now i don't understand is how can i use this array concept with this inheritance. since every extended class has different parameters.
I did this last assignment with your help and others here.
last assignment:
public class Polynomial
{
private int[] coeff = new int[5];
public Polynomial()
{
coeff[0] = 0;
coeff[1] = 0;
coeff[2] = 0;
coeff[3] = 0;
coeff[4] = 0;
}
public Polynomial(int c0, int c1, int c2, int c3, int c4)
{
this.coeff[0] = c4;
this.coeff[1] = c3;
this.coeff[2] = c2;
this.coeff[3] = c1;
this.coeff[4] = c0;
}
public Polynomial(Polynomial p)
{
coeff = (int[])p.coeff.clone();
}
public int getCoeff(int i)
{
return this.coeff[i];
}
public boolean setCoeff(int pos, int num)
{
if (pos <= 4 || pos >= 0 )
{
coeff[pos] = num;
return true;
}
else
return false;
}
public String toString()
{
String s = "";
for (int c = 4; c >=0; c--)
{
if (coeff[c] !=0)
if (c != 0)
{
if (coeff[c] < 0 && c!=4)
s = s + coeff[c] + "X^" + c + " ";
if (coeff[c] > 0 && c!=4)
s = s + "+ " + coeff[c] + "X^" + c + " ";
if (c == 4)
s = s + coeff[c] + "X^" + c + " ";
}
else
{
if (coeff[c] > 0)
s = s + "+ " + coeff[c];
else
s = s + coeff[c];
}
}
return s;
}
public boolean equals(Polynomial p)
{
if ((p.coeff[4] != coeff[4]) ||(p.coeff[3] != coeff[3]) ||(p.coeff[2] != coeff[2]) ||(p.coeff[1] != coeff[1]) ||(p.coeff[0] != coeff[0]) )
return false;
return true;
}
public Polynomial add(Polynomial p)
{
return (new Polynomial(p.coeff[4]+coeff[4],p.coeff[3]+coeff[3],p.coeff[2]+coeff[2],p.coeff[1]+coeff[1],p.coeff[0]+coeff[0]));
}
public Polynomial derive()
{
return (new Polynomial(0,coeff[4]*4,coeff[3]*3,coeff[2]*2,coeff[1]));
}
public double evaluate(double x)
{
return (coeff[4] * x*x*x*x) +
(coeff[3] * x*x*x) +
(coeff[2] * x*x) +
(coeff[1] * x) +
coeff[0];
}
public int numberOfTerms()
{
int n = 0;
if (coeff[4] != 0)
n +=1;
if (coeff[3] != 0)
n +=1;
if (coeff[2] != 0)
n +=1;
if (coeff[1] != 0)
n +=1;
if (coeff[0] != 0)
n +=1;
return n;
}
}
The driver of pervious assignment
import java.util.*;
public class Driver
{
public static void main(String[] args)
{
System.out.println("*~*~*~*Program writen by *~*~*~*");
Scanner input = new Scanner(System.in);
Polynomial[] poly = new Polynomial[8];
int[] tmp = new int[5];
System.out.println("Enter a polynomial");
for(int i=0; i<5;i++)
{
System.out.print("Coefficient for X^" + i + ": ");
int coeff = input.nextInt();
tmp[i] = coeff;
}
poly[0] = new Polynomial(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
System.out.println("Enter a polynomial");
for(int i=0; i<5;i++)
{
System.out.print("Coefficient for X^" + i + ": ");
int coeff = input.nextInt();
tmp[i] = coeff;
}
poly[1] = new Polynomial(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
Random obj= new Random();
for(int i=0; i<5;i++)
{
int coeff = obj.nextInt(100);
tmp[i] = coeff;
}
poly[2] = new Polynomial(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
for(int i=0; i<5;i++)
{
int coeff = obj.nextInt(100);
tmp[i] = coeff;
}
poly[3] = new Polynomial(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
for(int i=0; i<5;i++)
{
int coeff = obj.nextInt(100);
tmp[i] = coeff;
}
poly[4] = new Polynomial(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
poly[5] = new Polynomial(poly[0].add(poly[2]));
poly[6] = new Polynomial(poly[1]);
for (int i = 0; i <= 4; i++)
{
int m = poly[6].getCoeff(i);
m *=3;
boolean b = poly[6].setCoeff(i,m);
}
poly[7] = new Polynomial(poly[4].derive());
System.out.print("Enter a double value for X: ");
double x = input.nextDouble();
for (int p = 0; p < 8; p++)
{
System.out.println("Polynomial " + p + " : " + poly[p] +
" has " + poly[p].numberOfTerms() +
" terms and evaluates to " + poly[p].evaluate(x) +
" for x = " + x);
}
for (int p = 1; p < 8; p++)
{
if ( poly[0].equals(poly[p]) )
System.out.println("Polynomial 0 and Polynomial " + p + " are equal.");
else
System.out.println("Polynomial 0 and Polynomial " + p + " are not equal.");
}
poly[5] = new Polynomial(poly[1]);
if ( poly[5].equals(poly[1]) )
System.out.println("Polynomial 5 and Polynomial 1 are now equal after the change.");
}
}
|
|
|
|
|
what a mess - where to start?
poly[0] = new Polynomial(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
that's evil. Please use:
poly[0] = new Polynomial(tmp);
and create a constructor
public Polynomial(int[] values) {
this.coeff = values;
}
than there is a neat chance for a loop as your code does several times the same thing:
8System.out.println("Enter a polynomial");
for(int i=0; i<5;i++)
{
System.out.print("Coefficient for X^" + i + ": ");
int coeff = input.nextInt();
tmp[i] = coeff;
}
poly[0] = new Polynomial(tmp);
This looks much better when done in a loop:
for(int iCount=0; iCount<=poly.length; iCount++){
for(int i=0; i<5;i++) {
System.out.print("Coefficient for X" + i + ":");
int coeff = input.nextInt();
tmp[i] = coeff;
}
poly[iCount] = new Polynomial(tmp);
}
regards Torsten
I never finish anyth...
|
|
|
|
|
I went through all this with him before. Nothing sunk in.
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
|
|
|
|
|
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.
|
|
|
|
|