0

I'm taking an introduction to java programming course at university and have an exam next week. I'm going through past exam papers am sort of stuck on this question:

Consider the following class X: class X { private boolean a; private int b; ... } 

(i)     Write a constructor for this class. [2 marks] 


(ii)   Show how to create an object of this class. [2 marks] 


(iii)  Add a method out, which returns b if a is true, and -b otherwise. This method must be usable for any client of 
    this class. [2 marks] 

I've included my code below, but what i'm stuck on is in the final part to this question. How does one call a method on a new object (as we haven't been taught that in class)? Or, does the question imply that the method has to be usable with any object, not just the created object?

Sorry for my awful code and dumb question, i'm really struggling with Java.

public class X {
 private boolean  a;
 private int b;

 X(final boolean i, final int j) {
  a = i;
  b = j;

 }

 static int Out(boolean a, int b) {
 if (a == true) {
  return b;
 }
  return -b;
 }

 public static void main(String[] args) {;

 X object1 = new X(true, 5);

 System.out.println(Out(object1));


 }
}
8
  • Take care of Java naming convention. Method names must start with lower case character Commented Jan 11, 2018 at 12:04
  • out should not be static and shouldn't take any params. Than you can call it like object1.out() Commented Jan 11, 2018 at 12:05
  • Java Nuts and Bolts - Using Objects: Calling an Object's Methods - You might want to work through the whole tutorial on classes. Also I don't think you already finished the first two questions correctly. You are probably meant to create a non static method of that class that uses the values set in the constructor, not a static method that just uses the passed parameters. Commented Jan 11, 2018 at 12:06
  • @ArifMustafa Yep, but i've been preparing for the past 4 weeks, going through my lecturers presentations. Now, i'm going through past exam papers (y) Commented Jan 11, 2018 at 12:07
  • Do not uae static methods if you are working on an object. Useobject1.Out() to get the desired value an rewrite the method to public int getB(); Commented Jan 11, 2018 at 12:07

2 Answers 2

1

You're very close to the solution. Simply make a method like this:

public int out() {
    if (a) {
        return b;
    } else {
        return -b;
    }
}

Then you can call it in your main method like this:

X object1 = new X(true, 5);
System.out.println(object1.out());

NB: remove the semicolon at the end of public static void main(String[] args) {;

Sign up to request clarification or add additional context in comments.

Comments

0

I think you were meant to create a non-static method named out, which can be called by the client of the class (any place where you create a new object of type X) using the dot notation

public int out() {
   if(a)
      return b;
   else
      return -b;
}

public static void main(String[] args) {
   X object1 = new X(true, 5);
   int result = object1.out();
   System.out.println(result);
}

Comments