wood burning stoves*
The moose likes Java in General and the fly likes New class not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "New class not working" Watch "New class not working" New topic
Author

New class not working

Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
Can anybody give me a hint as to why when I use :


for this class:


it gives me:
> run DemoTV
TVClass@143724e
TVClass@749436
>


Am I missing something obvious? If I re run it multiple times, the numbers always come out differently too...
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
If you want to print the brand, price and size, you have to call the getter methods using the TVClass object.

But still if you want to use the toString() then override the toString() method and just give the objects of the TVClass in the print statement.

But the getter methods do not confine to the standard specifications. And there is no setter method in first place to set the values.

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
The TVClass shall look like below. I have put the constructor for you, but you can remove it and use the setter methods alone.

Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
wow thanks, I tried plugging it in to see what would happen though and got all this mess:

8 errors found:
File: C:\Users\Chris\Desktop\Java Programming Programs\java\DemoTV.java [line: 8]
Error: C:\Users\Chris\Desktop\Java Programming Programs\java\DemoTV.java:8: cannot find symbol
symbol : constructor TVClass(java.lang.String)
location: class TVClass
File: C:\Users\Chris\Desktop\Java Programming Programs\java\DemoTV.java [line: 9]
Error: C:\Users\Chris\Desktop\Java Programming Programs\java\DemoTV.java:9: getBrand() in TVClass cannot be applied to (java.lang.String)
File: C:\Users\Chris\Desktop\Java Programming Programs\java\DemoTV.java [line: 10]
Error: C:\Users\Chris\Desktop\Java Programming Programs\java\DemoTV.java:10: getPrice() in TVClass cannot be applied to (double)
File: C:\Users\Chris\Desktop\Java Programming Programs\java\DemoTV.java [line: 11]
Error: C:\Users\Chris\Desktop\Java Programming Programs\java\DemoTV.java:11: getSize() in TVClass cannot be applied to (int)
File: C:\Users\Chris\Desktop\Java Programming Programs\java\DemoTV.java [line: 12]
Error: C:\Users\Chris\Desktop\Java Programming Programs\java\DemoTV.java:12: cannot find symbol
symbol : constructor TVClass(java.lang.String)
location: class TVClass
File: C:\Users\Chris\Desktop\Java Programming Programs\java\DemoTV.java [line: 13]
Error: C:\Users\Chris\Desktop\Java Programming Programs\java\DemoTV.java:13: getBrand() in TVClass cannot be applied to (java.lang.String)
File: C:\Users\Chris\Desktop\Java Programming Programs\java\DemoTV.java [line: 14]
Error: C:\Users\Chris\Desktop\Java Programming Programs\java\DemoTV.java:14: getPrice() in TVClass cannot be applied to (double)
File: C:\Users\Chris\Desktop\Java Programming Programs\java\DemoTV.java [line: 15]
Error: C:\Users\Chris\Desktop\Java Programming Programs\java\DemoTV.java:15: getSize() in TVClass cannot be applied to (int)


Isn't it supposed to take the values I put in and print them back out?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
You have to match the number of parameters in the method with the number of arguments you pass while calling it.

In the DemoTV class check how you are many arguments you pass to the constructor. And while calling the geXXX method you don't need to pass any arguments.
Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
I figured out what I was doing wrong, but now I'm back to square one, its still giving me random values
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
You could post both the class' code you have tried.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
TVClass@143724e - This is not a random value. Instead of giving tv1 in the print statement, try to give the getter such as tv1.getBrand(). I doubt if you have modified your TVClass in first place.
Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
I just did it with the one you gave me, if I tell it to print brand it works, but it won't print the whole thing out as a string, let me ask this, is it not going to? Am I trying to get java to do something it simply won't do?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
you can... see my first post about overriding the Object:toString() method of object.

Add the below method in the TvClass and try to print the object in the print statement.
Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
Wow......................................... I knew to do that, I just totally spaced out... Thanks for saving me yet again John. Here's everything:




> run DemoTV
The TV is a Sony it costs 1200.0 dollars and is a 55.0 inch TV
The TV is a LG it costs 700.0 dollars and is a 42.0 inch TV
The TVs are not the same
Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
Chris Haris wrote:Wow......................................... I knew to do that, I just totally spaced out... Thanks for saving me yet again John. Here's everything:




> run DemoTV
The TV is a Sony it costs 1200.0 dollars and is a 55.0 inch TV
The TV is a LG it costs 700.0 dollars and is a 42.0 inch TV
The TVs are not the same


now just for fun, I changed size from a double to an int, because normally TVs are measured in whole inches... why doesn't line 9 like that? and how could I change it so it doesn't give me an error?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
By line #9 i believe you mean the constructor - if so you try to pass a double value from your demo class but try to store that in your size variable declared as int in TvClass.

If you change a type, you have to change the type also in affected constructors and the methods.
Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
Oh ok, that makes sense. Thanks a lot I appreciate it.
Chandan Sarma
Greenhorn

Joined: Oct 04, 2011
Posts: 14
Hi ,

you need to modified your getter and setter method some what and as for my understand no need of constructor is there.



Using this you are able to print any thing what you want. i am printing the tv1 "brand " and tv2 "size"

the Out put is given as follow

compile-single:
run-main:
the brand isSony
the size is9.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: New class not working
 
Similar Threads
compare() and compareTo()
Variable declaration
problem with the StreamCorruptedException
printOut help
Need help on inheritance...