Question

Add on to the java code below to include a welcome message and menu for the...

Add on to the java code below to include a welcome message and menu for the user. The user should be able to decide when to exit the program ( the user can enter his data included in code below ), and then can either exit, OR enter another one. Need a loop structure in this code.

import java.util.Scanner;
public class ReduceFraction {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a numerator: ");
int n = in.nextInt();
System.out.print("Enter a denominator: ");
int d = in.nextInt();
if (n < d) {
System.out.printf("%d / %d is a proper fraction\n", n, d);
} else if (n % d == 0) {
System.out.printf("%d / %d is an improper fraction and it can be reduced to %d\n", n, d, n/d);
} else {
System.out.printf("%d / %d is an improper fraction and its mixed fraction is %d + %d / %d\n", n, d, n/d, n%d, d);
}
}
}

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

Java code

============================================================================================

package shape;

import java.util.Scanner;

public class ReduceFraction {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner in = new Scanner(System.in);
       System.out.println("Welcome to fraction find: ");
       char ch='y';
      
      
      
       while(ch=='y')
       {
          
               System.out.print("Enter a numerator: ");
               int n = in.nextInt();
               System.out.print("Enter a denominator: ");
               int d = in.nextInt();
               if (n < d) {
               System.out.printf("%d / %d is a proper fraction\n", n, d);
               } else if (n % d == 0) {
               System.out.printf("%d / %d is an improper fraction and it can be reduced to %d\n", n, d, n/d);
               } else {
               System.out.printf("%d / %d is an improper fraction and its mixed fraction is %d + %d / %d\n", n, d, n/d, n%d, d);
               }
      
           System.out.print("Do u want to enter another set of numbers?(press y for again): ");
           ch=in.next().charAt(0);
          
       }
      
       System.out.println("--PROGRAM ENDS--");
          

   }

}

============================================================================================

Output

Add a comment
Know the answer?
Add Answer to:
Add on to the java code below to include a welcome message and menu for the...
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
  • Must be written in JAVA Code Modify Fig. 19.2 to use recursive method recursiveLinear-Search to perform...

    Must be written in JAVA Code Modify Fig. 19.2 to use recursive method recursiveLinear-Search to perform a linear search of the array. The method should receive the search key and starting index as arguments. If the search key is found, return its index in the array; otherwise, return –1. Each call to the recursive method should check one index in the array. Figure 19.2 import java.security.SecureRandom; import java.util.Arrays; import java.util.Scanner; public class LinearSearchTest{ public static int linearSearch(int data[], int searchKey)...

  • For this code after case 0, I need it to ask the user if they would...

    For this code after case 0, I need it to ask the user if they would like to see the menu again and some type of if statement or loop to ask them yes or no and if yes print again and if no end program. So it would ask if they would like to see the menu again then ask what their choice is. If yes print menu and if no end program. import java.util.InputMismatchException; import java.util.*; import java.io.*;...

  • The Calculator Class Create an application dlass called Colcustor with the following members Methods Retuns firtNumber...

    The Calculator Class Create an application dlass called Colcustor with the following members Methods Retuns firtNumber secondumber static void mainsering Asks for two integer undan uiet", ..",ЛТеп shows the result according to Note Use the attached example program and add required methods. Then une a switch tructure in the try black to call proper method according to the input operator Sample Output 1 : 2074 Sample Output 2 D1VLdeBy LerOWLthEkceptionHahaling-Jav / Handling ArithmeticExceptions and InputMismatchExceptions. import java.util.InputMismatchException; import java.util.Scanner; public...

  • Need help with Java for Fraction exercise

    Add another public method called add to your Fraction class. This method adds another fraction to the ‘calling object’. Thus, the method will take a Fraction class object as a parameter, add this parameter fraction to the calling object (fraction), and return a Fraction object as a result. HINT: We can use cross multiplication to determine the numerator of the resultant Fraction. The denominator of the resultant Fraction is simply the multiplication of the denominators of the two other Fractions.Add...

  • Please, modify the code below to use the Switch Statements in Java instead of “if” statements...

    Please, modify the code below to use the Switch Statements in Java instead of “if” statements to make the decisions. import java.util.Scanner; public class Area { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter code(C for circle, R for rectangle, S for square): "); char code = in.next().charAt(0); if(code == 'C') { System.out.print("Enter radius: "); double radius = in.nextDouble(); System.out.println("Area of circle is " + (Math.PI * radius * radius)); } else if(code == 'R') {...

  • Step 4: Add code that discards any extra entries at the propmt that asks if you...

    Step 4: Add code that discards any extra entries at the propmt that asks if you want to enter another score. Notes from professor: For Step 4, add a loop that will validate the response for the prompt question:       "Enter another test score? (y/n): " This loop must only accept the single letters of ‘y’ or ‘n’ (upper case is okay). I suggest that you put this loop inside the loop that already determines if the program should collect...

  • I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation...

    I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation problem: at EvenOdd.main(EvenOdd.java:10) I am unable to find what I can do to fix this issue. Can you please assist? My code is below: import java.util.InputMismatchException; import java.util.Scanner; public class EvenOdd {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        System.out.println("Welcome to this Odd program!");        int userInput1 = input.nextInt();    }       public...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • JAVA: Rewrite the following code to where the inputs are from a file. The file name...

    JAVA: Rewrite the following code to where the inputs are from a file. The file name will be from the input from the user scanner. CODE: import java.util.*; public class Q1 { public static int sumOD (int k) { int sumOD = 0; int in = k; while (in != 0) { int digitON = in % 10; sumOD += digitON; in /= 10; } return sumOD; }    public static void main(String[] args) { int n, i, k; System.out.println("Enter...

  • Java Write a Fraction class that implements these methods:  add ─ This method receives a...

    Java Write a Fraction class that implements these methods:  add ─ This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction.  multiply ─ This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction.  divide ─ This method receives a Fraction parameter and divides the parameter fraction by the calling object fraction.  print ─ This method prints the fraction using fraction notation (1/4, 21/14, etc.)...

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