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
  • 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]+" ");       ...

  • /** * * Jumping frog game: user enters road length and series of jumps and *...

    /** * * Jumping frog game: user enters road length and series of jumps and * the program displays the current position of the frog. */ public class hw4_task5 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int playAgain = 1; int roadLen; System.out.println("JUMPING FROG"); while (playAgain == 1){ System.out.printf("Enter the length of the road: "); roadLen = in.nextInt(); play(roadLen); // Starts a new game with a road this long. System.out.printf("\nDo you want to play again...

  • Write a while loop that prints all positive numbers that are divisible by 10 and less...

    Write a while loop that prints all positive numbers that are divisible by 10 and less than a given number n. For example, if n is 100, print 10 20 30 40 50 60 70 80 90. import java.util.Scanner; public class PositiveLess {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        System.out.print("n: ");        int n = in.nextInt();        ...        while (...)        {           ...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • I need help on creating a while loop for my Java assignment. I am tasked to...

    I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...

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