Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In a class called Lab5, implement all of the following methods as specied. Part

ID: 3771792 • Letter: I

Question

In a class called Lab5, implement all of the following methods as specied.

Part 1 Suppose you nd a magic $1.00 coin. Its magic power is as follows: as each day passes, you get an addi-tional dollar plus half of what you already had (it appears by the window somehow). Write a method called getRichQuick (no input is necessary for this method) that prints the rst n days while your total is less than $1,000,000 dollars. In other words, how many days does it take for you to earn $1,000,000? Your program should calculate these numbers and print the following output: Day 1: $1 Day 2: $1 + ($1 + .50) = $2.50 Day 3: $2.50 + ($1 + 1.25) = $4.75 ... Day N: $X + ($1 + Y) >= $1000000

Part 2 Write a method called eTaylor that takes as input (i.e. argument) a double x and returns the value of ex. This method should NOT use any Math library functions, so you will need to use Taylor Series for ex (loop until terms become close to 0, say 10e-16):

ex = 1 + x + x2 2! + x3 3! + x4 4! + ...

Part 3
Write a void method that takes a String as argument. This argument represents the name of a le containing some words, one per line. The method should have functionality to read the le which contains a word in each line, check whether or not the word is a palindrome and print to the screen all of the palindromes contained in the input le, one per line.
Also, the last line of the output should have the message: “There are x palindromes out of y words in le words.txt” (where x is the number of palindrome words in the input le, and y is the total number of words in the input le). Hint: for this lab exercise you will need the following methods on String objects: length() -gives the length of a string (that is, the number of characters it contains) and charAt(i) - gives the character at position i. For example:
int sizeOfString = "bob".length () // should be 3 char firstChar = "bob".charAt (0) // should be ’b’

Part 4
In main write a menu that allows a user of your program to select and run the programs you wrote in the methods above. The user should be able to play with your methods a many times as they want and only exit the program if they want to exit. For example:

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 an x: 1 e^1 = 2.7182818284590452
What is your choice? 0 Thanks for participating! Goodbye.
Note that the methods from part 2 and 3 did not have you asking for keyboard input. Do NOT go back and modify your methods from part 2 and 3, instead handle the request for keyboard input (to get the x or the name of the text le) in main after the user has made his/her choice (i.e. item 2 or 3 in the menu).

Explanation / Answer

Here is the code for you. If you have any further queries, just get back to me.

import java.io.*;
import java.util.*;
class Lab5Functions
{
public static void getRichQuick()
{
int day = 1;
double totalAmount = 1;
System.out.println("Day "+day+": $"+totalAmount);
while(totalAmount < 1000000)
{
day++;
System.out.printf("Day %d: $%.2f + ($1 + %.2f) = $", day, totalAmount, totalAmount/2);
totalAmount += 1 + (totalAmount/2);
System.out.printf("%.2f ", totalAmount);
}
}

public static double eTaylor(double x)
{
double ePowerX = 0, termNumber = 0, termValue = 1;
while(termValue >= 0.000001)
{
double powerX = 1;
for(int i = 1; i <= termNumber; i++)
powerX *= x;
double fact = 1;
for(int i = 1; i <= termNumber; i++)
fact *= i;
termValue = powerX/fact;
ePowerX += termValue;
//System.out.println("TermNumber: "+termNumber+" TermValue: "+termValue+" ePowerX: "+ePowerX);
termNumber++;
}
return ePowerX;
}
public static void palindromes(String fileName) throws IOException
{
BufferedInputStream file = new BufferedInputStream(new FileInputStream(fileName));
int count = 0, palindromes = 0;
String str, reverse= "";
while((str=file.readLine())!=null)
{
count++;
int length = str.length();

for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + str.charAt(i);

if (str.equals(reverse))
{
System.out.println(str);
palindromes++;
}
}
System.out.println("There are "+palindromes+" palindromes out of "+count+" words in le "+fileName);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while(true)
{
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. ");
System.out.println("What is your choice? ");
int choice = sc.nextInt();
switch(choice)
{
case 0: System.out.println("Thanks for participating! Goodbye."); System.exit(0);
case 1: getRichQuick(); break;
case 2: System.out.print("Enter an x: ");
   int x = sc.nextInt();
   System.out.println("e^"+x+" = "+eTaylor(x));
   break;
case 3: System.out.print("Enter the name of the file: ");
       String fileName = sc.next();
case 4:   break;
default: System.out.println("Invalid menu option. ");
}
}
}
}