-2

i failed and now i need to take a resit exam. This question was on the final exam and i feel like similar one will come on resit exam. We cant use our computers on the exam we have to write everything on paper so that was big issue i couldnt understand if im doing it wrong or right. Please be kind while answering because im new at java and i dont want to lose my hope i need to pass.

(i cant remember %100 but this was the main idea)

Q: In this question write two methods. First method should take two random variables. And second method should compare those two random variables and gives the result as higher one.

My solution:

import acm.program.*;
import acm.util.RandomGenerator;
public class RandomGenereratorBaby extends ConsoleProgram {

public RandomGenerator rgen = RandomGenerator.getInstance();{

int x = rgen.nextInt(0,9);
int y = rgen.nextInt(0,9);

}
public char BiggerOneWins (int x, int y){
    if(x>=y){
    return (char) (x)   ;

    }else{
        return (char) (y);
    }
}}

Please help me.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

0

So my interpretation of the question says the method should be taking in the variables and storing them, not making some random numbers. So my solution would look something like this

public class RandomGenereratorBaby extends ConsoleProgram {

    private int x;
    private int y;

    public void takeInNumbers(int x, int y){
        this.x = x;
        this.y = y;
    }

    public int biggerOneWins(){
        if(x>=y){
            return x;
        }else{
            return y;
        }
    }
}

I've also changed the return type to int as returning as char seemed a little odd. This is my interpretation of the question and the answer I would give. I hope that helps

Maltanis
  • 513
  • 1
  • 5
  • 13
  • i understand what you did thank you so much for your answer. But is it wrong how i used the RandomGenerator ? Even if its not usefull here it can be useful on the resit exam. – omrfarukc Jan 23 '18 at 16:12
  • @omrfarukc I'd say yes, as you didn't actually create a method to take in the two variables. Your code just creates two random numbers and then has a method that takes in two numbers and compares them. This doesn't seem to do what they ask for in your question. – Maltanis Jan 23 '18 at 16:15