Question

Need help to condense this code block.     public java.lang.String getResult()    {        this.getArraySum();...

Need help to condense this code block.

    public java.lang.String getResult()
   {
       this.getArraySum();
      
       if (this.sumOfArray > this.guessTargNumber)
       {
           if (this.guess == 'Y')
           {
               return "You guessed correctly! The sum was " + this.sumOfArray + "!";
           }
           else
           {
               return "You guessed wrong! The sum was " + this.sumOfArray + "!";
           }
       }
       else
       {
           if (this.guess == 'Y')
           {
               return "You guessed wrong! The sum was " + this.sumOfArray + "!";
           }
           else
           {
               return "You guessed correctly! The sum was " + this.sumOfArray + "!";
           }
       }
   }

It needs to be 2 or less returns. Maybe a local variable can be added and used?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

It is very simple to do that by using a local variable.

here, I have used output as a local variable.

I hope this code will be useful for you.

public java.lang.String getResult()
{      
this.getArraySum();
  
String output=" ";  

if (this.sumOfArray > this.guessTargNumber)
{
if (this.guess == 'Y')
{
output="You guessed correctly! The sum was " + this.sumOfArray + "!";  
}
else
{
output="You guessed wrong! The sum was " + this.sumOfArray + "!";
}
}
else
{
if (this.guess == 'Y')
{
output="You guessed wrong! The sum was " + this.sumOfArray + "!";  
}
else
{
output= "You guessed correctly! The sum was " + this.sumOfArray + "!";
}
}

return output;
}

Add a comment
Know the answer?
Add Answer to:
Need help to condense this code block.     public java.lang.String getResult()    {        this.getArraySum();...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need Help finishing up MyCalendar Program: Here are the methods: Modifier and Type Method Desc...

    Need Help finishing up MyCalendar Program: Here are the methods: Modifier and Type Method Description boolean add​(Event evt) Add an event to the calendar Event get​(int i) Fetch the ith Event added to the calendar Event get​(java.lang.String name) Fetch the first Event in the calendar whose eventName is equal to the given name java.util.ArrayList<Event> list() The list of all Events in the order that they were inserted into the calendar int size() The number of events in the calendar java.util.ArrayList<Event>...

  • PLEASE HELP! The assignment details are in the *noted part of the code. I REALLY need...

    PLEASE HELP! The assignment details are in the *noted part of the code. I REALLY need help. import java.util.LinkedList; public class TwoDTree { private TwoDTreeNode root; private int size; public TwoDTree() {    clear(); } /** * Returns true if a point is already in the tree. * Returns false otherwise. * * The traversal remains the same. Start at the root, if the tree * is not empty, and compare the x-coordinates of the point passed * in and...

  • this code below is not the full code. I need help outputting both first name and...

    this code below is not the full code. I need help outputting both first name and last name. Can you please fix my code. I use getline but it says sometthing is wrong with my if statement our whatever cout<<" -=| ADDING STUDENT |=-" << endl; cout<<"Please enter the student’s name: "; cin>>s_name; if(g.add_std(s_name)) cout<<"\n"<<s_name<<" was successfully added to the gradebook!"; else cout<<"\nStudents cannot be added because the gradebook is full!"; break;

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please write the code according to functions assigned...

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed for this assignment, so don't use them. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please...

  • Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** *...

    Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** * Program starts with this method. * * @param args A String to be printed backwards */ public static void main(String[] args) { if (args.length == 0) { System.out.println("ERROR: Enter a String on commandline."); } else { String word = args[0]; String backwards = iterativeBack(word); // A (return address) System.out.println("Iterative solution: " + backwards); backwards = recursiveBack(word); // B (return address) System.out.println("\n\nRecursive solution: " +...

  • help with java OOP, here is the started code: package P2; public class Farm {   ...

    help with java OOP, here is the started code: package P2; public class Farm {    private double availableFood;    private Animal[] animals;    public Farm() {        setAvailableFood(1000);        animals = new Animal[4];        animals[0] = new Chicken();        animals[1] = new Cow();        animals[2] = new Llama();        animals[3] = new Llama();    }    public void makeNoise(){           // all animals make their sound (Moo, Cluck, etc)        for(Animal...

  • I need help with this code This is what I need to do: Implement the Stack...

    I need help with this code This is what I need to do: Implement the Stack Class with an ArrayList instead of an array, including the following functions: • empty • push • peek • pop • overrided toString( ) function which returns all of the stack’s contents Things to note: • You no longer need a size. • You no longer need to define a constant DEFAULT_CAPACITY. Since ArrayLists grow dynamically. • Whenever possible, use ArrayList functions instead of...

  • \\ this is the code i need help to get this part working the rest works...

    \\ this is the code i need help to get this part working the rest works but this and im not sure what is missing or how to make it work. this is what they asking for to do and there is two errors the ones shown in the pictures this is all the information I have from zybooks\\ Q3. (54 Points) Complete a public class to represent a Movie as described below. (a-e 4 pts each) (f-h 8 pts...

  • Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the...

    Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the following data entry error conditions: A number less than 1 or greater than 12 has been entered for the month A negative integer has been entered for the year utilizes a try and catch clause to display an appropriate message when either of these data entry errors exceptions occur. Thank you let me know if you need more info import java.util.*; //Class definition public...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT