• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

New class not working

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I figured out what I was doing wrong, but now I'm back to square one, its still giving me random values
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could post both the class' code you have tried.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh ok, that makes sense. Thanks a lot I appreciate it.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic