In: Computer Science
Java Codes:
1. Create a class named Proficiency1Practice In the main method, first ask the user to enter a short sentence which ends in a period. Use Java String class methods to determine the following about the sentence and display in the console: • How many total characters are in the sentence? • What is the first word of the sentence? Assume the words are separated by a space. • How many characters are in the first word of the sentence? Second, ask the user to enter three separate words and display them in alphabetical order.
2. Create a class named Proficiency2Practice In the main method write code that takes two numbers from the Java console representing, respectively, an investment, and an interest rate. Then calculate and output the future value of the investment, in dollars and cents, using the following formula: future value = investment * ( 1 + interest rate )year Assume that the interest rate is an annual rate and is compounded annually, and that year is a whole number greater than or equal to 1. Your program should display the value of the investment in 5, 10 and 20 years. Both of the inputs need to be positive values, so use an if statement to test the user input. If a negative value was entered, display a message to the user, convert the input to a positive value, and continue with your program. (Later in the semester, you’ll learn how to loop and repeat the request for input when there is a problem with an input value.)
Thanks for the question. Below is the code for both the problems. Let me know if you have any doubts or if you need anything to change. Thank You !! =========================================================================== import java.util.Scanner; public class Proficiency1Practice { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a short sentence: "); String sentence = scanner.nextLine(); System.out.println("Total characters in the sentence: " + sentence.length()); // splitting the sentence using space delimiter String words[] = sentence.split("\\s+"); System.out.println("First word in the sentence: " + words[0]); System.out.println("First word \"" + words[0] + "\" contains " + words[0].length() + " characters."); System.out.print("Enter first word: "); String firstWord = scanner.nextLine(); System.out.print("Enter second word: "); String secondWord = scanner.nextLine(); System.out.print("Enter third word: "); String thirdWord = scanner.nextLine(); if (firstWord.compareTo(secondWord) <= 0 && firstWord.compareTo(thirdWord) <= 0) { if (secondWord.compareTo(thirdWord) <= 0) { System.out.println("Printing in alphabetically order: " + firstWord + ", " + secondWord + ", " + thirdWord); }else{ System.out.println("Printing in alphabetically order: " + firstWord + ", " + thirdWord + ", " + thirdWord); } }else if (secondWord.compareTo(firstWord) <= 0 && secondWord.compareTo(thirdWord) <= 0) { if (firstWord.compareTo(thirdWord) <= 0) { System.out.println("Printing in alphabetically order: " + secondWord + ", " + firstWord + ", " + thirdWord); }else{ System.out.println("Printing in alphabetically order: " + secondWord + ", " + thirdWord + ", " + firstWord); } }else{ if (firstWord.compareTo(secondWord) <= 0) { System.out.println("Printing in alphabetically order: " + thirdWord + ", " + firstWord + ", " + secondWord); }else{ System.out.println("Printing in alphabetically order: " + thirdWord + ", " + secondWord + ", " + firstWord); } } } }
==========================================================================
import java.util.Scanner; public class Proficiency2Practice { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double investment = 0, interestRate = 0.0; System.out.print("Enter investment amount: $"); investment = scanner.nextDouble(); System.out.print("Enter interest rate (%): "); interestRate = scanner.nextDouble(); if (investment < 0) { System.out.println("You have entered -ve investment amount. Converting it to positve."); investment = -investment; } if (interestRate < 0) { System.out.println("You have entered -ve interest rate. Converting it to positve."); interestRate = -interestRate; } System.out.println("Future Value in 5 years : $" + investment * Math.pow(1 + interestRate / 100, 5)); System.out.println("Future Value in 10 years : $" + investment * Math.pow(1 + interestRate / 100, 15)); System.out.println("Future Value in 20 years : $" + investment * Math.pow(1 + interestRate / 100, 20)); } }
==========================================================================