CSC 15 chapter 3 lab, part 2, Spring 2017 Write a program which has the followin
ID: 3862721 • Letter: C
Question
CSC 15 chapter 3 lab, part 2, Spring 2017
Write a program which has the following methods besides the main method:
1. Write a method named site. This method accepts the Scanner object as its parameter. this method reads a commercial website URL that starts with www and ends with .edu or .com, ..this method retrieves the name of the site and outputs the result. If the user enters www.yahoo.com then this method outputs yahoo.
2. Write a method called decrypt that accepts a Scanner object as its parameter. this method reads an encrypted word and then decrypts the word. Here is the decryption algorithm: only the even numbered characters should be counted as the part of the word. Other characters should be discarded. For example if the user enters: hwealxlaod then the decrypted word is HELLO. Pay attention that the decrypted word is all in capital letters
3. Write a method called reverse that accepts the Scanner object as its parameter. This method asks the user for the entire name and prints the name in the reverse order. You must only use nextLine() method. If the user enters Mary Lumbardi then the method should output: Lumbardi Mary
4. Write a method called futureValue that accepts the Scanner object as its parameter. This method will ask the user for an investment, an interest rate and years of the investment. Then the method should calculate the future value of the investment. Here is the formula to calculate the future investment:
Future value = investment * ( 1+ interest rate) ^years
5. main method: in this method you need to ask the user how many times he/she wants to run each method and create a for loop for each of the method class. In the main you need to have three for loops for each of the methods except the method reverse. You must create a Scanner object so that you can pass it to different methods. Only one scanner object is needed for the whole program.
Output: Your program must create the exact same output. Sample output: You must generate this output exactly
How many different website do you have > 3
Enter the URL of the website > www.sierracollege.edu The name of the website you entered is > sierracollege
Enter the url of the website > www.csus.edu The name of the website you entered is > csus
Enter the url of the website > www.yahoo.com
The name of the website you entered is > yahoo
******************
How many encrypted words do you have:3 Enter the encrypted word > skckhtowozlw Here is the decrypted word > SCHOOL
Enter the encrypted word > htudnrgsrdys Here is the decrypted word > HUNGRY
Enter the encrypted word > hiejlzl3ow Here is the decrypted word > HELLO
******************
Enter your first and last name separated by a space > Mary Lambardi Here is your name in the reverse order: Lambardi Mary
******************
How many different investment do you have > 3
Enter the investment > 500
Enter the interest rate > .065 Enter the number of the years > 5
With an investment of 500.0
at an interest rate of 0.065 compounded annually: the future value in 5 years is 685
Enter the investment > 900
Enter the interest rate > .078
Enter the number of the years > 10
With an investment of 900.0
at an interest rate of 0.078 compounded annually: the future value in 10 years is 1907
Enter the investment > 800
Enter the interest rate > .012
Enter the number of the years > 15
With an investment of 800.0
at an interest rate of 0.012 compounded annually: the future value in 15 years is 957
Explanation / Answer
/**
* @(#)chegg1.java
*
*
* @author
* @version 1.00 2017/3/4
*/
import java.util.Scanner;
public class chegg1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("How many different website do you have > ");
int count;
count=in.nextInt();
in.nextLine();//for nextLine method to work properly
while(count>0){
site(in);
count=count-1;
}
System.out.println("******************");
System.out.println("How many encrypted words do you have: ");
count=in.nextInt();
in.nextLine();
while(count>0){
decrypt(in);
count=count-1;
}
System.out.println("******************");
reverse(in);
System.out.println("******************");
System.out.println("How many different investment do you have > ");
count=in.nextInt();
while(count>0){
futureValue(in);
count=count-1;
}
// TODO code application logic here
}
public static void site(Scanner in){
System.out.println("Enter the URL of the website > ");
String url = in.next();
in.nextLine();
String name="";
int i=0;
while(url.charAt(i)!='.') i++;
i++;
while(url.charAt(i)!='.'){
name = name + url.charAt(i);
i++;
}
System.out.printf("The name of the website you entered is > " + name +" ");
}
public static void decrypt(Scanner in){
System.out.println("Enter the encrypted word > ");
String message = in.next();
in.nextLine();
String encrypt="";
int i=0;
while(i<message.length()){
if(i%2==0) encrypt = encrypt + message.charAt(i);
i++;
}
System.out.printf("Here is the decrypted word > " + encrypt.toUpperCase()+" ");
}
public static void reverse(Scanner in){
System.out.print("Enter your first and last name separated by a space > ");
String name = in.nextLine();
String firstname="";
String lastname="";
int i=0;
while(name.charAt(i)!=' '){
firstname = firstname + name.charAt(i);
i++;
}
i++;
while(i<name.length()){
lastname = lastname + name.charAt(i);
i++;
}
System.out.printf("Here is your name in the reverse order: "+lastname+" "+firstname+" ");
}
public static void futureValue(Scanner in){
double investment,years;
double interest_rate;
System.out.println("Enter the investment > ");
investment = in.nextDouble();
System.out.println("Enter the interest rate > ");
interest_rate = in.nextDouble();
System.out.println("Enter the number of the years > ");
years = in.nextDouble();
double Future_value = (investment * Math.pow((1+interest_rate),years));
System.out.printf("With an investment of %.1f at an interest rate of %.3f compounded annually: the future value in %.0f years is %.0f ",
investment,interest_rate,years,Future_value);
}
}