In Java, Multiple String Operations With Single Scanner Object

 Problem Statement:

  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 site without the www prefix and the domain suffix. 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. Use Math.round() to round the result to a whole number. 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.

import java.util.Scanner;
import java.util.StringTokenizer;

public class Main{

	private static void site(Scanner in){
		System.out.print("Enter the url of the website > ");
		String url = in.nextLine();
		url = url.toLowerCase();
		url = url.replace("www.", "");
		url = url.replace(".edu", "");
		url = url.replace(".com", "");
		System.out.printf("The name of the website you entered is > %s%n", url);
	}

	private static void decrypt(Scanner in){
		System.out.print("Enter the encrypted word > ");
		String word = in.nextLine();
		StringBuilder sb = new StringBuilder();
		for(int i = word.length()-1; 0 <= i; i --){
			if(0 == i%2) sb.insert(0,Character.toUpperCase(word.charAt(i)));
		}
		System.out.printf("Here is the decrypted word > %s%n", sb);
	}

	private static void reverse(Scanner in){
		System.out.print("Enter your first and last name separated by a space > ");
		String line = in.nextLine();
		StringTokenizer st = new StringTokenizer(line);
		StringBuilder sb = new StringBuilder();
		boolean added = false;
		while(st.hasMoreTokens()) {
			if(added) sb.insert(0, ' ');
			sb.insert(0, st.nextToken());
			added = true;
		}
		System.out.printf("Here is your name in the reverse order: %s%n", sb);
	}

	private static void futureValue(Scanner in){
		System.out.print("Enter the investment > ");
		double amount = new Double(in.nextLine());
		System.out.print("Enter the interest rate > ");
		float rate = new Float(in.nextLine());
		System.out.print("Enter number of the years > ");
		int years = new Integer(in.nextLine());
		double value = amount * Math.pow((1+rate), years);
		System.out.printf("With an investment of %.1f at an interest rate of %.3f compounded annually: the future value in %d years is %.0f%n", amount, rate, years, value);
	}

	public static void main(String[]args){
		Scanner stdin = new Scanner(System.in);
		int num;
		System.out.print("How many different website do you have > ");
		num = new Integer(stdin.nextLine());
		while(0 < num --) site(stdin);
		System.out.print("How many encrypted words do you have: ");
		num = new Integer(stdin.nextLine());
		while(0 < num --) decrypt(stdin);
		reverse(stdin);
		System.out.print("How many different investment do you have > ");
		num = new Integer(stdin.nextLine());
		while(0 < num --) futureValue(stdin);
	}
}
Download

Comments

Popular posts from this blog