Problem in assigning a variable value to another one

hi

i am just trying to develop class calculating the selling price of car as 15% of the purchase price addeed on the purchase price

and i developed the next code which is a part of longer code :

package cardealer;

publicclass Car4Saleextends Car{

publicstaticvoid main(int[] args){

Car4Sale pur , sell;//pur is purchasing price , sell is selling price

sell = pur *(1+ 0.15) ;

}

}

the proplem is in the line : sell = pur *(1+ 0.15) ;

the program says: the variable pur might have not been intialized . operator * can not be applied .

Note: when i delete this line the variable pur returns intialized in the upper line

[1112 byte] By [scrolldowna] at [2007-11-26 18:50:40]
# 1
You're using pur before you've set a value for it. That doesn't make sense.
paulcwa at 2007-7-9 6:24:42 > top of Java-index,Java Essentials,New To Java...
# 2

what exactly is this program doing?

you dont have any values for the sale/purchasing price. Do you want the user to input the purchase price?

if so you would need to do something like this:

String purchase = JOptionPane.showInputDialog(null, "enter the purchase price of your car");

Int pur = integer.parseInt(purchase);

put that just before you do the calculation.

boblettoj99a at 2007-7-9 6:24:42 > top of Java-index,Java Essentials,New To Java...
# 3

> what exactly is this program doing?

> you dont have any values for the sale/purchasing

> price. Do you want the user to input the purchase

> price?

>

> if so you would need to do something like this:

> > String purchase = JOptionPane.showInputDialog(null,

> "enter the purchase price of your car");

> Int pur = integer.parseInt(purchase);

>

>

> put that just before you do the calculation.

thank you

i didnt add GUI code because i was intending to develop it in an other class .

But stil there is a problem because i want a code to assign purchase price of different cars that to be entered.

How to assign purchase price for its correspondent car which is to be represented or stored in super class car

scrolldowna at 2007-7-9 6:24:42 > top of Java-index,Java Essentials,New To Java...
# 4
anybody has an idea how to assign the purchase price to car
scrolldowna at 2007-7-9 6:24:42 > top of Java-index,Java Essentials,New To Java...
# 5
I'm not sure if I understand your problem: do you want the Car class to store information about the purchasing prices of different cars and you want to be able to access them in your subclass?
manuel.leiriaa at 2007-7-9 6:24:42 > top of Java-index,Java Essentials,New To Java...
# 6

> I'm not sure if I understand your problem: do you

> want the Car class to store information about the

> purchasing prices of different cars and you want to

> be able to access them in your subclass?

Actually the selling and purchasing price is in the subclass . What i want is to assign the purchase price to its correspondent car

scrolldowna at 2007-7-9 6:24:42 > top of Java-index,Java Essentials,New To Java...
# 7
Guev them an initial value of zero or whatever they should be. This will get rid of your error.if you need a value from the super class then it should be automatically available to you providing it isnt private.
kikemellya at 2007-7-9 6:24:43 > top of Java-index,Java Essentials,New To Java...
# 8

> Guev them an initial value of zero or whatever they

> should be. This will get rid of your error.

>

> if you need a value from the super class then it

> should be automatically available to you providing it

> isnt private.

there is an object called car which have many attributes

what i want is to assign new attribute which is purchase price to it.

this is what i am wondering about right now

scrolldowna at 2007-7-9 6:24:43 > top of Java-index,Java Essentials,New To Java...
# 9

Here is a simple example of what (i think) your trying to do.

//Car class

public class Car

{

private double purchasePrice;

private String type;

public Car(double price, String type)

{

this.purchasePrice=price;

this.type=type;

}

public double getPurchasePrice()

{

return purchasePrice;

}

public void setPurchasePrice(int price)

{

this.purchasePrice = price;

}

public String getType()

{

return type;

}

public void setType(String type)

{

this.type = type;

}

}

//Car Sales class

public class CarSales

{

private Car car = new Car(100,"Porsche");

private double sellingPrice;

public static void main(String[] args)

{

CarSales cs = new CarSales();

System.out.println("Car Type :" +cs.car.getType());

System.out.println("Car Purchase Price :"+cs.car.getPurchasePrice());

System.out.println("Car Selling price :"+cs.getSellingPrice(cs.car.getPurchasePrice()));

}

public double getSellingPrice(double purchasePrice)

{

return purchasePrice += (purchasePrice / 100) *15;

}

}

modify it to suit your needs.

kikemellya at 2007-7-9 6:24:43 > top of Java-index,Java Essentials,New To Java...
# 10

> Here is a simple example of what (i think) your

> trying to do.

>

> > //Car class

> public class Car

> {

> private double purchasePrice;

> private String type;

>

> public Car(double price, String type)

> {

> this.purchasePrice=price;

> this.type=type;

> }

>

> public double getPurchasePrice()

> {

> return purchasePrice;

> }

>

> public void setPurchasePrice(int price)

> {

> this.purchasePrice = price;

> }

>

> public String getType()

> {

> return type;

> }

>

> public void setType(String type)

> {

> this.type = type;

> }

>

> }

>

>

>

> //Car Sales class

> public class CarSales

> {

> private Car car = new Car(100,"Porsche");

> private double sellingPrice;

>

> public static void main(String[] args)

> {

> CarSales cs = new CarSales();

>

> System.out.println("Car Type :"

> :" +cs.car.getType());

> System.out.println("Car Purchase Price

> ce :"+cs.car.getPurchasePrice());

> System.out.println("Car Selling price

> ce

> :"+cs.getSellingPrice(cs.car.getPurchasePrice()));

> }

>

> public double getSellingPrice(double purchasePrice)

> {

> return purchasePrice += (purchasePrice / 100) *15;

> }

>

> }

>

>

>

> modify it to suit your needs.

thank you for this great reply but what i am really looking for is to enter attributes for the car. the attributes are number , manufacturer , size, color . after then adding the purchase price in a subclass and calculating the selling price .

Ihave no problem whith input code but i am not sure about it is how to deal with these attributes

scrolldowna at 2007-7-9 6:24:43 > top of Java-index,Java Essentials,New To Java...
# 11

> thank you for this great reply but what i am really

> looking for is to enter attributes for the car. the

> attributes are number , manufacturer , size, color .

> after then adding the purchase price in a subclass

> and calculating the selling price .

> Ihave no problem whith input code but i am not sure

> about it is how to deal with these attributes

Just write them as a typical Javabean -- private fields, getter and setter methods -- on both the superclass and the subclass.The subclass will have more fields.

Like this:

public class Animal {

private String name;

public String getName() { return name; }

public void setName(String name) { this.name = name; }

}

public class Monkey extends Animal {

private int humansKilled;

public int getHumansKilled() {return humansKilled; }

public void setHumansKilled(int humansKilled) { this.humansKilled = humansKilled; }

}

Actually a "humans killed" attribute ought not to have getters/setters but anyway you get the point.

paulcwa at 2007-7-9 6:24:43 > top of Java-index,Java Essentials,New To Java...
# 12
can i intialise an array of variables ?what i mean i want to set attributes of car to be entered so i choose array
scrolldowna at 2007-7-9 6:24:43 > top of Java-index,Java Essentials,New To Java...
# 13

public class Car4Sale extends Car {

public static void main(int[] args) {

Car4Sale pur , sell; //pur is purchasing price , sell is selling price

sell = pur *(1+ 0.15) ;

}

}

the proplem is in the line : sell = pur *(1+ 0.15) ;

pur and sell are not numbers and they cannot be multiplied!

They are references to objects, use the field that stores the price, and make constructors.

sell.pruchasePrice=pur.pruchasePrice*(1+ 0.15) ;

if you run your code you will have nothing.

null=null*1.15!!!!!!!!!!!!!!!!!!

Objects need to be initialize to have values!

MelGohana at 2007-7-9 6:24:43 > top of Java-index,Java Essentials,New To Java...
# 14
> can i intialise an array of variables ?> what i mean i want to set attributes of car to be> entered so i choose arrayI have no idea what you're asking here.
paulcwa at 2007-7-9 6:24:43 > top of Java-index,Java Essentials,New To Java...
# 15

> > can i intialise an array of variables ?

> > what i mean i want to set attributes of car to be

> > entered so i choose array

>

> I have no idea what you're asking here.

yes what i meant that i have program to recieve information about each car and the information is multiple like size, color and number.

what i have tried is to store them in an array but unfortunately jbuilder couldnt accept this try and says variable have not initialised .

scrolldowna at 2007-7-21 17:30:19 > top of Java-index,Java Essentials,New To Java...
# 16
Its not a JBuilder problem, is bad code problem, if you read the error you will notice your fail!The values of the variables that you are trying to store may have not a value for example!int a;try: int a=0;Be sure to assign a value before saving them.
MelGohana at 2007-7-21 17:30:19 > top of Java-index,Java Essentials,New To Java...
# 17

> Its not a JBuilder problem, is bad code problem, if

> you read the error you will notice your fail!

>

> The values of the variables that you are trying to

> store may have not a value for example!

>

> int a;

>

> try: int a=0;

>

> Be sure to assign a value before saving them.

is GUI solve it ? when i add GUI input code does it solve the problem? because i dont want to assign a value the value to be entered from the user

Message was edited by:

scrolldown

scrolldowna at 2007-7-21 17:30:19 > top of Java-index,Java Essentials,New To Java...
# 18

Well, as I said before the variables must have a value to be used. A GUI is just a way to interact with the user. It doesnt matter if the values are assigned by the user, writen on the code or by console. What matters is that they must have a value to work.

BTW: Which language do you speak natively?

Message was edited by:

MelGohan

MelGohana at 2007-7-21 17:30:19 > top of Java-index,Java Essentials,New To Java...
# 19
if you want to initialize variables without enteringexplicitly value you can make it as instance variable then it will get default value for each type , even if it object default will be null
eaajea at 2007-7-21 17:30:19 > top of Java-index,Java Essentials,New To Java...
# 20

> > > can i intialise an array of variables ?

> > > what i mean i want to set attributes of car to

> be

> > > entered so i choose array

> >

> > I have no idea what you're asking here.

>

> yes what i meant that i have program to recieve

> information about each car and the information is

> multiple like size, color and number.

> what i have tried is to store them in an array but

> unfortunately jbuilder couldnt accept this try and

> says variable have not initialised .

If I understand you, and I have no idea whether I actually do, then maybe what you need is to put the car data in a file, and then write a class that will take that file, read the data, and create Car objects with that data. It could have a method that would create and return an array (or better yet a java.util.Collection) of Car objects.

Probably the easiest way to do this would be to use a java.util.Properties object, and load it with data from a file that has lines like this:

numberOfCars=10

car0.color=red

car0.make=Ford Compensator SUV

car1.color=blue

car1.make=Honda Schmonda

etc., and then your class could create property names following a well-defined pattern and looking them up in the Properties object.

Though actually XML would probably be more appropriate format for something like this. It would just be harder to implement.

paulcwa at 2007-7-21 17:30:19 > top of Java-index,Java Essentials,New To Java...
# 21

> if you want to initialize variables without entering

> explicitly value you can make it as instance variable

> then it will get default value for each type , even

> if it object default will be null

would u please give me an example ? and i will be happy if it is like what iam looking at (color, number , size ,price)

scrolldowna at 2007-7-21 17:30:19 > top of Java-index,Java Essentials,New To Java...
# 22
scrolldown, it's quite obvious that you're hopeless. You cannot be helped on a forum like this. You may want to consider that programming is not for you. Or at least, do tutorials and courses on your own and with other classmates.
warnerjaa at 2007-7-21 17:30:19 > top of Java-index,Java Essentials,New To Java...
# 23
you don't know how you make instance variable ?go and read google is best way to say how !!
eaajea at 2007-7-21 17:30:19 > top of Java-index,Java Essentials,New To Java...
# 24

> scrolldown, it's quite obvious that you're hopeless.

> You cannot be helped on a forum like this. You may

> want to consider that programming is not for you. Or

> at least, do tutorials and courses on your own and

> with other classmates.

I hope that i have an effort from you .

i jusr wrote :

String vrn , man , eng, col ;

and they still unintialized !

how to initialize them without give them any value

scrolldowna at 2007-7-21 17:30:19 > top of Java-index,Java Essentials,New To Java...
# 25

> > String vrn , man , eng, col ;

>

>

> and they still unintialized !

> how to initialize them without give them any value

The statement is in direct conflict with itself. It's like asking how do I drink water without actually drinking it.

warnerjaa at 2007-7-21 17:30:19 > top of Java-index,Java Essentials,New To Java...
# 26

> > > > String vrn , man , eng, col ;

> >

> >

> > and they still unintialized !

> > how to initialize them without give them any

> value

>

> The statement is in direct conflict with itself. It's

> like asking how do I drink water without actually

> drinking it.

wht to do ?! this is what i have been learned from the lecturer .

when you write( String ) you will initialize a variable of type string

scrolldowna at 2007-7-21 17:30:19 > top of Java-index,Java Essentials,New To Java...
# 27

dear

public class A{

int i ;

Strudent s;

public A(){

System.out.println("USE GOOGLE AND READ ");

}

}

i and s is instance variable

the default for int is zero , so when you use it there is no problem because its initialized by compiler , and s is object reference it is null until you make an object

s=new Student();

otherwise NULLPOINERException will appear

hope its fine now !

eaajea at 2007-7-21 17:30:19 > top of Java-index,Java Essentials,New To Java...
# 28
errr
mkoryaka at 2007-7-21 17:30:19 > top of Java-index,Java Essentials,New To Java...
# 29

> dear

> >

> public class A{

>

> int i ;

> Strudent s;

>

> public A(){

> System.out.println("USE GOOGLE AND READ ");

> }

>

> }

>

>

>

> i and s is instance variable

>

> the default for int is zero , so when you use it

> there is no problem because its initialized by

> compiler , and s is object reference it is null until

> you make an object

> > s=new Student();

>

>

> otherwise NULLPOINERException will appear

>

> hope its fine now !

I made this correction to see if i can store the instance variable( vrn ) in an arryay

public class Car {

public static void main(String args []) {

Car vrn , man , eng, col ; //vrn is vehicle reference number,

// man is manufacturer ,

//eng is engine size, col is car color.

vrn = new Car() ;

man = new Car() ;

eng = new Car() ;

col = new Car() ;

String cardetails [] = new String[6];

cardetails [1] = vrn;

}

}

but jbuilder showed that there is an error in line : cardetails [1] = vrn;

and the error is : incompatible typs found

scrolldowna at 2007-7-21 17:30:19 > top of Java-index,Java Essentials,New To Java...
# 30

Well, that's what happens when you try to stuff an Oreo cookie into a pencil sharpener.

Why on earth are you trying to put a Car object into a String array?

And if vrn is supposed to be a vehicle reference number, then why is it declared as a Car type? Why is col (vehicle color) also a Car type?

Are you just randomly typing java-like code and hoping for a miracle?

warnerjaa at 2007-7-21 17:30:24 > top of Java-index,Java Essentials,New To Java...
# 31
This is my new msn:Well, that's what happens when you try to stuff an Oreo cookie into a pencil sharpener.haha!Well they do say something about.... if a thousand noobs were left for a thousand years to code at a console, they would eventually write the entire Java
FatClienta at 2007-7-21 17:30:24 > top of Java-index,Java Essentials,New To Java...
# 32

hhhhhhhhhhh

thank you for all valuable clarification.

i will try to improve it and take care for your clarification then post it. but i made this because someone advised to set vrn as an instance variable if i want to use it without error

Message was edited by:

scrolldown

Message was edited by:

scrolldown

scrolldowna at 2007-7-21 17:30:24 > top of Java-index,Java Essentials,New To Java...
# 33

> if you want to initialize variables without entering

> explicitly value you can make it as instance variable

> then it will get default value for each type , even

> if it object default will be null

Yeah, but that's a terrible reason to change a local variable into a field.

paulcwa at 2007-7-21 17:30:24 > top of Java-index,Java Essentials,New To Java...
# 34

hi

i just made some changes as below :

package cardealer;

import java.util.Collection;

public class Car {

public static void main(String args []) {

Car car ;

car = new Car();

String vrn,man , eng, col ; //vrn is vehicle reference number ,

// man is manufacturer ,

//eng is engine size, col is car color.

car.man = "";

}

}

but jbuilder shows an errror massage on the last line : can not find symbol variable man .

i tried to solve it but i couldnt know where is the wrong this time .

scrolldowna at 2007-7-21 17:30:24 > top of Java-index,Java Essentials,New To Java...
# 35
The Car class doesn't have a "man" member. You do have a local variable inside the main() method named "man" though. You could set it.man = "";I know this is going to go nowhere though. I do hope you'll soon realize programming is really not for you. It just isn't.
warnerjaa at 2007-7-21 17:30:24 > top of Java-index,Java Essentials,New To Java...
# 36

> The Car class doesn't have a "man" member. You do

> have a local variable inside the main() method named

> "man" though. You could set it.

> man = "";

>

> I know this is going to go nowhere though. I do hope

> you'll soon realize programming is really not for

> you. It just isn't.

and why you are angry ?!!!

I am here looking for help not looking for evaluation.

I think this forum is for who is new to java , isnt it ?

scrolldowna at 2007-7-21 17:30:24 > top of Java-index,Java Essentials,New To Java...
# 37

You've gotten help, and you seemed completely unable to understand it.

Or maybe you just ignore it. Either way it's frustrating.

I'd strongly suggest that you read a very, very elementary book on computer programming using Java. You must learn things such as:

1) you can't assign values to a variable that doesn't exist

2) you can't assign a value to a variable that holds a different type of value

3) you can't use a value that doesn't exist

4) what a "variable" is

5) what a "type" is

6) what a "value" is

7) to write a program, you should first have a pretty clear idea what the program is supposed to do and why.

It would also help a lot if you learned how to express yourself better. It's still not entirely clear what you're trying to accomplish.

paulcwa at 2007-7-21 17:30:24 > top of Java-index,Java Essentials,New To Java...