Homework2
Note: Submit your work (upload the .java source code files ONLY, not the compiled .class files!) through the “Homework2” link on Brightspace. You may submit an unlimited number oftimes; we will only grade the last/latest submission attempt, but be sure to attach all of your files
to each submission attempt. Be sure to include your name and Stony Brook ID number in acomment at the beginning of each file that you submit.Due: Thursday, October 10, 11:59pm Total: 25 points (5 points per problem)
Submission Instructions: Name your java classes for this assignment as:
Problem1: Pyramid.java
Problem2: Interests.java
Problem3: LongestCommonPrefix.java
Problem4: PerfectNumber.java
Problem5: ArmstrongNumbers.java
- Pyramid.java: (Printing numbers in a pyramid pattern) Write down a program in Java with anested for loop that prints the following output (powers of 2) for any number of lines:Here is a sample run:Enter the number of lines: 8
Output:
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
- Interests.java: (Financial application: comparing loans with various interest rates) Write aprogram that lets the user enter the double loan amount and loan period in number of years(int) and displays the monthly and total payments for each interest ratestarting from 5% to 8%,with an increment of 1/8.Here is the sample run:Loan amount: 10000.00
- Number of years: 5
Interest Rate Monthly Payment Total Payment
𝑑𝑜𝑢𝑏𝑙𝑒 𝑚𝑜𝑛𝑡ℎ𝑙𝑦𝐼𝑛𝑡𝑒𝑟𝑒𝑠𝑡𝑅𝑎𝑡𝑒 = 𝑎𝑛𝑛𝑢𝑎𝑙𝐼𝑛𝑡𝑒𝑟𝑒𝑠𝑡𝑅𝑎𝑡𝑒 / 1200 ;𝑑𝑜𝑢𝑏𝑙𝑒 𝑚𝑜𝑛𝑡ℎ𝑙𝑦𝑃𝑎𝑦𝑚𝑒𝑛𝑡 = 𝑙𝑜𝑎𝑛𝐴𝑚𝑜𝑢𝑛𝑡 ∗ 𝑚𝑜𝑛𝑡ℎ𝑙𝑦𝐼𝑛𝑡𝑒𝑟𝑒𝑠𝑡𝑅𝑎𝑡𝑒 / (1 −(𝑀𝑎𝑡ℎ. 𝑝𝑜𝑤(1 / (1 + 𝑚𝑜𝑛𝑡ℎ𝑙𝑦𝐼𝑛𝑡𝑒𝑟𝑒𝑠𝑡𝑅𝑎𝑡𝑒), 𝑛𝑢𝑚𝑏𝑒𝑟𝑂𝑓𝑌𝑒𝑎𝑟𝑠 ∗ 12)));𝑑𝑜𝑢𝑏𝑙𝑒 𝑡𝑜𝑡𝑎𝑙𝑃𝑎𝑦𝑚𝑒𝑛𝑡 = 𝑚𝑜𝑛𝑡ℎ𝑙𝑦𝑃𝑎𝑦𝑚𝑒𝑛𝑡 ∗ 𝑛𝑢𝑚𝑏𝑒𝑟𝑂𝑓𝑌𝑒𝑎𝑟𝑠 ∗ 12;𝑆𝑦𝑠𝑡𝑒𝑚. 𝑜𝑢𝑡. 𝑝𝑟𝑖𝑛𝑡𝑓(“%. 3𝑓%% %. 2𝑓 %. 2𝑓\𝑛”, 𝑎𝑛𝑛𝑢𝑎𝑙𝐼𝑛𝑡𝑒𝑟𝑒𝑠𝑡𝑅𝑎𝑡𝑒,𝑚𝑜𝑛𝑡ℎ𝑙𝑦𝐼𝑛𝑡𝑒𝑟𝑒𝑠𝑡𝑅𝑎𝑡𝑒,𝑡𝑜𝑡𝑎𝑙𝑃𝑎𝑦𝑚𝑒𝑛𝑡);
- LongestCommonPrefix.java: Write a program in Java 代 写Pyramid Interests PerfectNumber ArmstrongNumbers that prompts the user to enter two stringsand display the largest common prefix of the two strings. If there are no common prefixbetween the two entered strings display a message which tells the user that the two stringdoesn’t have a common prefix.Here are some sample run:Enter the first string: AtlantaEnter the second string: MaconAtlanta and Macon have no common prefix.Enter the first string: Welcome to Java
Enter the second string: Welcome to programmingThe common prefix is: Welcome toNote: The prefix actually includes the space after ‘to’ as wellEnter the first string: I love coffee
Enter the second string: I love JavaThe common prefix is I love
- PerfectNumber.java: A positive integer is called a perfect number if it is equal to the sum ofall of its positive divisors, excluding itself. For example, 6 is the first perfect number because6 = 3 + 2 + 1. The next is 28 = 14 + 7 + 4 + 2 +1. Write a program that asks the user for anupper limit and prints all the perfect numbers up to that limit.Here is a sample run:
Enter the upper limit: 10000The perfect numbers below 10000 are: 6 28 496 8128.5. ArmstrongNumbers.java: An Armstrong number is an n-digit integer such that the sum of the𝑛 𝑡ℎ power of its digits is equal to the number itself. For example, 371 is an Armstrong numberbecause 3 3 + 7 3 + 1 3 = 371 (371 is a 3-digit number). 8208 is an Armstrongnumber because 8 4 + 2 4 + 0 4 + 8 4 = 8208 (8208 is a 4-digit number). Write a program thatasks the user for a lower limit and an upper limit and prints all theArmstrong numbers up tothat limit.
Here are some sample runs:
Enter the lower limit: 10
Enter the upper limit: 1000
The Armstrong numbers between 10 and 1000 are: 153 370 371 407
Enter the lower limit: 8000
Enter the upper limit: 20000
The Armstrong numbers between 8000 and 20000 are: 8208 9474
Enter the lower limit: 25000
Enter the upper limit: 100000
The Armstrong numbers between 25000 and 100000 are: 54748 92727 93084
没有回复内容