Question: CSSSKL142 Lab 5 More Loops Template: Lab5.java as follows: import java.util.Scanner; import java.util.*; /** * Description of this class here */ public class Lab5 {
CSSSKL142 Lab 5
More Loops
Template: Lab5.java
as follows:
import java.util.Scanner;
import java.util.*;
/**
* Description of this class here
*/
public class Lab5 {
public static void main(String[] args) {
// Test your methods by calling them here
getRichQuick(); // Should run your method
double taylorResult = eTaylor(3.5); // Will call your method
with a parameter. Now display the return value from your method
Scanner sc = new Scanner(System.in);
palindromeCheck(sc); // test your method
sc.close();
}
// Part 1
public static void getRichQuick() {
// Declare your variables here and implement your logic
}
// Part 2
public static double eTaylor( double x ) {
double taylorNumber = 0.0;
// Your logic goes here
return taylorNumber;
}
public static double eTaylorHelper(double x, int n){
double power = 1;
double factorial = 1;
for(int i = 1; i
power = power * x;
factorial = factorial * i;
}
return power / factorial;
}
// Part 3
/**
* This program reads words, identifies, counts and writes all the
palindromes and the total
* palindrome count.
*
* // hint 1: Using keybord.next() will only return what comes before
a space.
// hint 2: Using keybord.nextLine() automatically reads the entire
current line.
*/
public static void palindromeCheck(Scanner keyboard){
String someWord = ""; // Stores words read from user input
int count = 0; // keeps track of Palindrome words only
(define algorithm to count # of palindrome words
int total = 0; // Counts the total number of lines read from
the given text file
System.out.println(" Enter some words separated by white space");
// Ask for user input
// declare your Scanner object here
// for each word user enters
while (keyboard.hasNext()) {
someWord = keyboard.next(); // store each word in a
string variable and then do your operations
total++; // increment number of
words as you read each one
// #1. Code your logic for how to determine if a word is
Palindrome first, then complete # 2
System.out.println(" " + total + " " + someWord); // test
}
}
}
Summary
This lab contains four exercises. You must complete all exercises within the allotted time for the lab. Please write clear and well commented code. Topics covered today: More Loops and (File I/O next week). We will also continue to practice and improve on branching and keyboard input with Scanner.
In a class called Lab5, implement all of the following methods as specified.
Part 1
Suppose you find a magic $1.00 coin. Its magic power is as follows: as each day passes, you get an additional 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 first 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+x22!+x33!+x44!+...
Hint: Write a helper method for finding the nth power of a number x, divided by
n!=n(n1)(n2)...21that is, your helper method should accept an x and output
xnn!Your method eTaylor will return the value of
n=0xnn!
Hint: Try theseries at least to the point where n = 5, n = 10 and n = 20.
Part 3
Hint: User will enter certain number of random words, you need to read the input as string and evaluate based on the following instructions.
Write a void method called palindromeCheck that takes as argument a scanner. The method should have functionality to check whether or not the word is a palindrome and print to the screen all of the palindromes, one per line. Also, the last line of the output should have the message: There are x palindromes out of y words provided by user (where x is the number of palindrome words detected, and y is the total number of words entered by user). 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 enter palindrome words. 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 all the words) in main after the user has made his/her choice (i.e. item 2 or 3 in the menu).
2.1 n
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
