Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How do I complete this exercise? (MOOC)

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my code so far:


I have exercise 39.1 (one of the last on the bottom of the site) https://materiaalit.github.io/2013-oo-programming/part1/week-2/

I have error at line 13 that says that star cannot be resolved to a variable. Anyone can help to do that exercise or give link to place where I can find answers for all MOOC exercises?
 
Saloon Keeper
Posts: 9574
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Directions call for a printStars() method, but nowhere does it mention a stars() method. This is something you came up with on your own. So what did YOU intend for the variable 'star' to hold?
 
Rancher
Posts: 259
13
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look at line7, the stars method is defined to take String as input.


you call the stars method on line13, but you did not feed it a String.


the variable star on line7 only has scope inside the stars method.
the variable star on line13 is undefined.

so to fix your problem you could do it several ways.
#1: use a string literal when calling your method on line13

you could just as easily use any String as the input


#2: change the method declaration on line7 so it doesn't take any input arguments.

then you'd call the method like this:


#3: another way to fix it would be to declare a new variable named star which has scope in your printStars method. it would need to be of type String, since that's what your method on line7 was defined as taking.

variables are only usable in the block that they were declared, and you can reuse the same name in different code blocks. make sure to learn about local variables, global variables, and the scope resolution operator. you can also access variables from other classes using a dot, for example if you have a class named Galaxy and a class named Milkyway, you can do Galaxy.star or Milkyway.star to access the star variable inside those classes from another class. you can also use getters and setters to do it.
 
Carey Brown
Saloon Keeper
Posts: 9574
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...or
You could eliminate the method altogether as the instructions suggested. Then call print() inside of your while() loop.
 
S Fox
Rancher
Posts: 259
13
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the exercise he was doing just before this one was for how to call a method from another method, so i think he created the extra method just to keep practicing how to do it.
 
Bartender
Posts: 1891
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a quick way to print your stars. I left your code mostly intact, but I totally agree, the "stars()" method needs to go as it adds nothing to simply the code. A simple System.out.println() statement in your printStars() method would be more semantically logical.

In your code below I updated, I created a final static variable at the top for the star character.

I also made your helper methods private. You want to preserve "Encapsulation" as much as possible so keep methods private you don't need to access outside your classes.

I renamed your loop variable to be more self-documenting.

Hope this helps.



Code Prints:

*****
***
*********


-- mike
 
Sheriff
Posts: 17153
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
line 5 hard codes the character being printed out so the star parameter is pointless. Either use the parameter or get rid of it.
 
Mike London
Bartender
Posts: 1891
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wasn't going to rewrite the entire program for the OP (but of course you're correct).

-- mike
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic