Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to know that a method has been called and returning value of a method

807601Mar 16 2008 — edited Mar 16 2008
Hi, everyone! I have two questions. One is about making judgment about whether a method has been called or not; another one is about how to return "String value+newline character+String value" with a return statement.
Here are the two original problems that I tried to solve.
Write a class definition of a class named 'Value' with the following:

a boolean instance variable named 'modified', initialized to false

an integer instance variable named 'val'

a constructor accepting a single paramter whose value is assigned to the instance variable 'val'

a method 'getVal' that returns the current value of the instance variable 'val'

a method 'setVal' that accepts a single parameter, assigns its value to 'val', and sets the 'modified' instance variable to true, and

a boolean method, 'wasModified' that returns true if setVal was ever called.
And I wrote my code this way:
public class Value
{
 boolean modified=false;
 int val;
 public Value(int x)
 {val=x;}
  public int getVal()
  {return val;}
   public void setVal(int y)
   {
    val = y;
    modified = true;
    }
     public boolean wasModified()
     {
      if(val==y&&modified==true)
      return true;
      }
}
I tried to let the "wasModified" method know that the "setVal" has been called by writing:
if(val==y&&modified==true)
or
if(x.setVal(y))
I supposed that only when the "setVal" is called, the "modified" variable will be true(it's false by default) and val=y, don't either of this two conditions can prove that the method "setVal" has been called?
I also have some questions about the feedback I got
class Value is public, should be declared in a file named Value.java
public class Value
cannot find symbol
symbol : variable y
location: class Value
if(val==y&&modified==true)
*^*
*2 errors*
I gave the class a name Value, doesn't that mean the class has been declared in a file named Value.java*?
I have declared the variable y, why the compiler cann't find it? is it because y has been out of scale?



The other problem is:
Write a class named Book containing:

Two instance variables named title and author of type String.

A constructor that accepts two String parameters. The value of the first is used to initialize the value of title and the value of the second is used to initialize author .

A method named toString that accepts no parameters. toString returns a String consisting of the value of title , followed by a newline character, followed by the value of author .

And this is my response:
public class Book
{
 String title;
 String author;
  public Book(String x, String y)
   { title=x; author=y; }
   public String toString()
   {return title;
    return author;
    }
}
I want to know that is it ok to have two return statements in a single method? Because when I add the
return author;
to the method toString, the compiler returns a complain which says it's an unreachable statement.
Thank you very much!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 13 2008
Added on Mar 16 2008
9 comments
532 views