$29.99
COMPUTER SCIENCE 11A
PROGRAMMING IN JAVA
PROGRAMMING ASSIGNMENT 3
Overview:
This assignment tests your understanding of printf(), strings, for loops, parameters and
methods that return values. Modularity in your code is very important, YOU MUST USE
STATIC METHODS.
Write six programs to solve the following problems. Write each program in a different file
with extension .java.
Problem 1
Write a program to find a baseball player’s batting average. The program should ask the user to
enter the number of times the player was at bat and the number of hits earned. It should then display
the batting average to four decimal places.
Problem 2
Write a program that inputs two string variables, first and last, which the user should
enter with his or her name. First, convert both strings to all lowercase. Your program should
then create a new string that contains the full name in pig latin with the first letter
capitalized for the first and last name. Use only the pig latin rule of moving the first letter
to the end of the word and adding “ay”. Output the pig latin name to the screen.
For example, if the user inputs “Antonella” for the first name and “Dilillo” for the last
name, then the program should create a new string with the text “Ntonellaaay Ililloday”
and print it.
Problem 3
Write a program that asks the user to enter an exponent. Then calculate and print 2 to the
power of that exponent starting from 2^0 up to the user's number, inclusive. You may
assume that the number entered by the user is 0 or greater than 0.
Problem 4
Write a program that inputs the name, quantity, and price of three items. The name should not
contain spaces. Output a bill with a tax rate of 6.25%. All prices should be displayed with two
decimal places. The bill should be formatted in columns with 30 characters for the name, 10
characters for the quantity, 10 characters for the price, and 10 characters for the total. Sample input
and output is shown as follows:
Input name of item 1:
lollipops
Input quantity of item 1:
10
Input price of item 1:
0.50
Input name of item 2:
diet_soda
Input quantity of item 2:
3
Input price of item 2:
1.25
Input name of item 3:
chocolate_bar
Input quantity of item 3:
20
Input price of item 3:
0.75
Your bill:
Item Quantity Price Total
lollipops 10 0.50 5.00
diet_soda 3 1.25 3.75
chocolate_bar 20 0.75 15.00
Subtotal 23.75
6.25% sales tax 1.48
Total 25.23
Problem 5
The factorial function is used frequently in probability problems. The factorial of a positive integer
n (written n! and pronounced “n factorial”) is equal to the product of the positive integers from 1
to n. Write a program that asks for three integers, and displays the factorial of each of them. Print
the result in a tabular format as shown here:
Enter the first number: 1
Enter the second number: 3
Enter the third number: 5
1! = 1
3! = 6
5! = 120
Problem 6
The Fibonacci numbers are a sequence of integers in which the first two numbers are 0 and 1, and
each following number is the sum of the previous two. For example, the first ten Fibonacci numbers
are: 0 1 1 2 3 5 8 13 21 34
The mathematical definition of each k-th Fibonacci number is the following:
î
í
ì
= =
+ > = - -
0, 1
, 1
0 1
1 2
F F
F F k
F k k
k
Write a program that prompts the user for an integer n and then prints the first n Fibonacci numbers.
The printing of the results should be happening in the main method.
Problem 7
Write a program that prompts for two integers representing a number of rows and columns, and
prints a grid of integers from 1 to (rows * columns) in column major order.
For example, if the user enters 4 and 6, your program should produce the following output:
1 5 9 13 17 21
2 6 10 14 18 22
3 7 11 15 19 23
4 8 12 16 20 24
Guidelines:
For this assignment you should limit yourself to the Java features covered in class so far (lecture
15).
Grading:
You will be graded on
o External Correctness: The output of your program should match exactly what is expected.
Programs that do not compile will not receive points for external correctness.
o Internal Correctness: Your source code should follow the stylistic guidelines shown in
class. Also, remember to include the comment header at the beginning of your program.
Submission:
Create a folder named PA3 containing all your java files, zip the folder, and submit it via
Latte the day it is due, Wed, Oct 5 at 6:00pm.