• Post Reply Bookmark Topic Watch Topic
  • New Topic

Objects and Classes Help  RSS feed

 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

So I'm trying to wrap my mind around objects and classes. I've watched tutorial videos and studied Java examples, but am still having issues.
I understand the concept, but I don't understand how to apply it to my situation.

For a task, I'm given a Java class.



I'm told to do the following:
a) Create 4 objects of the above class:
b) Add all four objects of the class to an array, assigning each a different genre. Genres to be used are:
o Landscape
o Portraiture
o Still Life
o History

c) Loop through the array and print the genre of each painting.
d) Loop through the array and print only the objects whose genres comprise a single word.
e) Again looping through the array, remove the object whose genre is History.
f) Finally, in a loop, print the genres of all painting objects now left in the array.

I believe e) and f) are reasonably straightforward, as I've done similar things with a string, but I simply can't figure out how to properly construct the Object in the first place.

This is what I have so far, but I don't think i'm actually even using the class provided, and when I try to do it by following a tutorial, I encounter errors.



Can someone more knowledgeable than myself in this area please give me some advice please?

Regards


 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cory Marcus wrote:...This is what I have so far, but I don't think i'm actually even using the class provided...

Yes you are right. You are not using the class provided.
Ok let us start rectifying from the first line in main method.
Instead of this

Use this

This will use the class provided.

Now try to rectify other things and post if you have any more doubts.
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tapas Chand,

Although I now receive the error "incompatible types: String cannot be converted to Painting"

 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes correct. Your array contains objects of Painting class. You cannot assign String to Painting.
You have to create Painting objects through new keyword and assign them and while creating an object, you have to use the constructor to set the value.
I hope you have learnt about constructors and how to use them.
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see.
This has resolved the issue.

I haven't done Java in a few weeks, and have simply been trying to catch up.
I've had experience with constructors, but have forgotten some things.

I now have an output of:

assignment1.Painting@659e0bfd
assignment1.Painting@2a139a55
assignment1.Painting@15db9742
assignment1.Painting@6d06d69c
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can modify your code to get proper output.
Hint: Use the method getGenre()
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming I use the toString method somehow?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is one of options.
You need to either override toString() or change println to print getGenre() as Tapas suggested.
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right.
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help everyone, I actually really appreciate it.

I've never used the two methods mentioned previously, and I'm encountering errors left, right and centre.

Can somebody post an example of the Syntax of either of these print methods?

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Previously your print statement was:
System.out.println(paintings[i]);

Within this statement paintings[i] is an instance of your Painting class.

You need to call a method on the instance.
You do it like this: instance.method().
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Painting[i].getGenre();

That is what Pawel said.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:Painting[i].getGenre();

That is not quite right.
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong here?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The array is named paintings, not Painting.

I am looking at the newest version of OP's code.
I just noticed the name has changed and was Painting previuosly.
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OOPS..sorry I misread the name.
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

So I've made a fair bit of progress on the code.

I've encountered a bit of hassle with the section requiring me to apply several requirements to an object for it to print. (I want to print the objects which are single words, and objects which are not "History")

It's kind of late, and I've been working on this a while. I know it's going to be something pretty simple, such as a bracket. Any help is appreciated.

 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what is the issue here?

 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies, I should've explained myself better.

The last section of code is meant to replace "History" with a "-", in addition to "Still Life". It's only managing to replace Still Life, producing:

Landscape
Portraiture
-
History
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is exactly why proper indentation is so important.
I indented your code. I have also added some curly braces. Always put curly braces, even when there is only one statement inside an if.

Take a look:Can you see the problem?

You also should fix this...
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Paweł said, i.e. your problem is with braces.

 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once again, thanks for the assistance.

I've now nearly completed the code.


However, now I'm being told that instead of simply not printing the variables, we actually have to set them to "null".

Is this simply:



Because I've replaced the "print -" lines with the null statement, and I encounter errors.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What errors?
Show us your code and error messages.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you familiar with the use of array initialisers?That will ensure there are no nulls in your array, and that the size of the array exactly matches its contents.
You usually need to write new Painting[] after the = but it you declare and initialise the array in one line, you are allowed to miss out new Painting[].

If you are at the beginner's stage where you still need to learn about arrays and printing, you might do well to omit the package declarations.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!