part 1 to 4 pleaseSolution The java program Lab5 that disp

part 1 to 4 please

Solution


/**
* The java program Lab5 that display a menu of chocies
* and prompts user to input for corresponding method
* and prints the results to console.
* */
//Lab5.java
import java.io.File;
import java.util.Scanner;
public class Lab5 {
  
   //create an instance of static Scanner class
   private static Scanner scanner=
           new Scanner(System.in);

   public static void main(String[] args) {
      
       boolean repeat=true;
       int choice = 0;
       //calling menu method
       menu();          
       //call getInput
       choice=getInput();
       while(repeat)
       {                  
           switch(choice)
           {
           case 1:
               //calling getRichQuick
               getRichQuick();
               break;
           case 2:
               int x;
               System.out.println(\"Enter an x:\");
               x=Integer.parseInt(scanner.nextLine());
               //calling eTaylor
               System.out.printf(\"e^ %d = %2.16f \",x,eTaylor((double)x));
               break;
           case 3:
               System.out.println(\"Enter name of text file : \");
               String fileName=scanner.nextLine();
              
               //calling getPalindromeCount
               getPalindromeCount(fileName);
               break;
           case 4:
               //calling menu
               menu();              
               break;
           case 0:
               //exit the program
               System.out.println(\"Thanks for participating! Goodbye.\");
               System.exit(0);
           }
           menu();          
           choice=getInput();
       }
      
      
   }
  
   //Takes string input and prints the number of palindrome words in
   //text file
   public static void getPalindromeCount(String fileName)
   {
       Scanner fileReader=null;
      
       try
       {
           fileReader=new Scanner(new File(fileName));
          
           String word;
           int count=0;
           while(fileReader.hasNextLine())
           {
               word=fileReader.nextLine();
               if(isPalindrome(word))
                   count++;
           }
          
           fileReader.close();
          
           System.out.printf(\"Number of palindrome words are %d \ \",count);
          
          
       }
       catch (Exception e)
       {
           System.out.println(e.getMessage());
       }
   }
  
  
   /**
   * Method that returns true of the string ,str is palindrome
   * otherwise returns false
   * */
   public static boolean isPalindrome(String str)
   {
       String reverse = new StringBuffer(str).reverse().toString();
              
       if(str.equals(reverse))
           return true;
       else
           return false;
   }
  
   /**
   * The method prints the day and corresonding amount to console
   * until amount is less than or equal to 1000000
   * */
   public static void getRichQuick()
   {
       double dollar=1;
       int day=1;
       final double TOTAL=1000000;
      
       while(dollar<TOTAL)
       {
           System.out.printf(\"Day %d : $ %4.2f\ \",day,dollar);
           dollar=dollar+(dollar/2.0);
           day++;
          
       }
      
       System.out.printf(\"Day %d : $ %4.2f\ \",day,dollar);
   }
  
   //prints a menu of choices
   public static void menu()
   {      
       System.out.println(\"Welcome to Lab5!\");
       System.out.println(\"Enter 1 to check how long it takes to get rich on a magic dollar coin.\");
       System.out.println(\"Enter 2 to calculate e^x for any real x.\");
       System.out.println(\"Enter 3 to find palindrome words in a text file.\");
       System.out.println(\"Enter 4 to re-print the menu.\");
       System.out.println(\"Enter 0 to exit.\");
   }
  
  
   //Takes a x value and prints the e^x
   public static double eTaylor(double x)
   {
      
       double ACCURACY =10e-16;
       int n=1;
       double term=1,sum=1;
       while (n <= 100)
       {
              term = term * x / n;
              sum = sum + term;                    
              if (term < ACCURACY)
                 n = 999;
              else
                 n = n + 1;
           }
      
       return sum;
      
   }
  
  
   //Read input from user
   public static int getInput()
   {
       System.out.println(\"What is your choice?\");
       int choice=Integer.parseInt(scanner.nextLine());      
       return choice;      
   }
  
  
}


---------------------------------------------

sample input.txt

jasmin
madam
camel

Output:

Welcome to Lab5!
Enter 1 to check how long it takes to get rich on a magic dollar coin.
Enter 2 to calculate e^x for any real x.
Enter 3 to find palindrome words in a text file.
Enter 4 to re-print the menu.
Enter 0 to exit.
What is your choice?
1
Day 1 : $ 1.00
Day 2 : $ 1.50
Day 3 : $ 2.25
Day 4 : $ 3.38
Day 5 : $ 5.06
Day 6 : $ 7.59
Day 7 : $ 11.39
Day 8 : $ 17.09
Day 9 : $ 25.63
Day 10 : $ 38.44
Day 11 : $ 57.67
Day 12 : $ 86.50
Day 13 : $ 129.75
Day 14 : $ 194.62
Day 15 : $ 291.93
Day 16 : $ 437.89
Day 17 : $ 656.84
Day 18 : $ 985.26
Day 19 : $ 1477.89
Day 20 : $ 2216.84
Day 21 : $ 3325.26
Day 22 : $ 4987.89
Day 23 : $ 7481.83
Day 24 : $ 11222.74
Day 25 : $ 16834.11
Day 26 : $ 25251.17
Day 27 : $ 37876.75
Day 28 : $ 56815.13
Day 29 : $ 85222.69
Day 30 : $ 127834.04
Day 31 : $ 191751.06
Day 32 : $ 287626.59
Day 33 : $ 431439.88
Day 34 : $ 647159.82
Day 35 : $ 970739.74
Day 36 : $ 1456109.61
Welcome to Lab5!
Enter 1 to check how long it takes to get rich on a magic dollar coin.
Enter 2 to calculate e^x for any real x.
Enter 3 to find palindrome words in a text file.
Enter 4 to re-print the menu.
Enter 0 to exit.
What is your choice?
2
Enter an x:
1
e^ 1 = 2.7182818284590455 Welcome to Lab5!
Enter 1 to check how long it takes to get rich on a magic dollar coin.
Enter 2 to calculate e^x for any real x.
Enter 3 to find palindrome words in a text file.
Enter 4 to re-print the menu.
Enter 0 to exit.
What is your choice?
3
Enter name of text file :
input.txt
Number of palindrome words are 1
Welcome to Lab5!
Enter 1 to check how long it takes to get rich on a magic dollar coin.
Enter 2 to calculate e^x for any real x.
Enter 3 to find palindrome words in a text file.
Enter 4 to re-print the menu.
Enter 0 to exit.
What is your choice?

part 1 to 4 pleaseSolution /** * The java program Lab5 that display a menu of chocies * and prompts user to input for corresponding method * and prints the resu
part 1 to 4 pleaseSolution /** * The java program Lab5 that display a menu of chocies * and prompts user to input for corresponding method * and prints the resu
part 1 to 4 pleaseSolution /** * The java program Lab5 that display a menu of chocies * and prompts user to input for corresponding method * and prints the resu
part 1 to 4 pleaseSolution /** * The java program Lab5 that display a menu of chocies * and prompts user to input for corresponding method * and prints the resu
part 1 to 4 pleaseSolution /** * The java program Lab5 that display a menu of chocies * and prompts user to input for corresponding method * and prints the resu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site