Place all codes under the method main.
1. Input 5 numbers and satisfy the ff:
a) sum of all input numbers
b) count of odd and even numbers except zero
c) count of positive, negative, and zeroes
d) count of divisible by 5
2. Generate 20 random numbers from 50-100 only and determine the ff:
a) the number of times 100 occurs
b) what are the lowest and highest generated numbers
3. Convert #1 to methods:
methods: main - calls method series
series - declares the array and its size
- calls the different methods below
input - input 5 numbers
output - display the array elements
sum - compute and display the sum
oddeven - count the odd and even numbers, display the results
posnegzero - count the positive, negative, and zeroes, display the results
div5 - count the numbers that are divisible by 5, display the result
4. Convert #2 to methods:
methods: main - calls method randomnumbers
randomnumbers - declares the array and its size
- call the methods below:
fill - populate the array with random numbers
display - display the array elements
t100 - counts the number of times 100 occurs, display the result
highlow - determines the lowest and highest numbers (the 1st element of the array is regarded as the lowest and the highest value from the start), display the results
Java
Lunes, Pebrero 18, 2019
Sabado, Pebrero 9, 2019
Activity #16 - Random Number, Method, Recursion
int n1 = (int) (Math.random() * 3); //generates numbers 0-2 only
int n2 = (int) (Math.random() * 3+1); //generates numbers 1-3 only
GUESSING GAME
#1 Guess the random number from 1-3 only.
methods:
main - calls method guess
guess - generates a random number
- accepts user's input
- determines if the user's guess is 'correct' or 'not correct'
- ask the user to Try Again (recursion)
input - validates user's input (1-3 only) (apply recursion)
solution:
public static void main(String args[])
{ guess( );
}
static void guess( )
{ System.out.println("Input 1-3 only");
int u = input( );
int r = (int) (Math.random() * 3+1);
System.out.println("Random number is "+r);
if (u == r)
System.out.println("Correct");
else
System.out.println("Not Correct");
System.out.println("Try Again? Press 'Y' to continue.");
char ch = sc.next( ).toUpperCase( ).charAt(0);
if (ch == 'Y')
guess( );
else
return;
}
static int input( )
{ int u = sc.nextInt( );
if (u<1 || u>3)
input( );
else
return u;
}
#2 Scoring the guessing game
methods:
main - calls method guess
guess - generates a random number
- accepts user's input
- determines if the user's guess is 'correct' or 'not correct', update the score if 'correct'
- ask the user to Try Again (apply recursion),
- before exit, display the total score/the number of tries
input - validates user's input (1-3 only) (apply recursion for invalid input)
int n2 = (int) (Math.random() * 3+1); //generates numbers 1-3 only
GUESSING GAME
#1 Guess the random number from 1-3 only.
methods:
main - calls method guess
guess - generates a random number
- accepts user's input
- determines if the user's guess is 'correct' or 'not correct'
- ask the user to Try Again (recursion)
input - validates user's input (1-3 only) (apply recursion)
solution:
public static void main(String args[])
{ guess( );
}
static void guess( )
{ System.out.println("Input 1-3 only");
int u = input( );
int r = (int) (Math.random() * 3+1);
System.out.println("Random number is "+r);
if (u == r)
System.out.println("Correct");
else
System.out.println("Not Correct");
System.out.println("Try Again? Press 'Y' to continue.");
char ch = sc.next( ).toUpperCase( ).charAt(0);
if (ch == 'Y')
guess( );
else
return;
}
static int input( )
{ int u = sc.nextInt( );
if (u<1 || u>3)
input( );
else
return u;
}
#2 Scoring the guessing game
methods:
main - calls method guess
guess - generates a random number
- accepts user's input
- determines if the user's guess is 'correct' or 'not correct', update the score if 'correct'
- ask the user to Try Again (apply recursion),
- before exit, display the total score/the number of tries
input - validates user's input (1-3 only) (apply recursion for invalid input)
HIGH/LOW
methods:
main - calls method highlow
guess - generates a random number from 0-10 only
- accepts user's input
- determines if the user's guess is 'correct' or 'not correct', update the score if 'correct'
- ask the user to Try Again (apply recursion),
- before exit, display the total score/the number of tries
input - validates user's input (0-1 only, 0 means below 6, 1 means above 5 ) (apply recursion for invalid input)
guess - generates a random number from 0-10 only
- accepts user's input
- determines if the user's guess is 'correct' or 'not correct', update the score if 'correct'
- ask the user to Try Again (apply recursion),
- before exit, display the total score/the number of tries
input - validates user's input (0-1 only, 0 means below 6, 1 means above 5 ) (apply recursion for invalid input)
Biyernes, Pebrero 8, 2019
Recursion - Activity #15
input: Employee name, employee code, days worked
output: Employee name, gross pay, total deduction, net pay
where: gross pay = days worked * rate
total deduction = sss + tax
sss = 10% of gross pay
tax = 15% of gross pay
net pay = gross pay - total deduction
rate table: employee code rate
1 530
2 550
3 570
methods:
main - calls employee method
employee - performs the input requirements, calls salary method, performs recursion for Try Again
salary - performs all computations, calls method output
output - displays the required outputs
code - validates the input code (1-3 only)
days - validates the input days worked (0-12 only)
output: Employee name, gross pay, total deduction, net pay
where: gross pay = days worked * rate
total deduction = sss + tax
sss = 10% of gross pay
tax = 15% of gross pay
net pay = gross pay - total deduction
rate table: employee code rate
1 530
2 550
3 570
methods:
main - calls employee method
employee - performs the input requirements, calls salary method, performs recursion for Try Again
salary - performs all computations, calls method output
output - displays the required outputs
code - validates the input code (1-3 only)
days - validates the input days worked (0-12 only)
Recursion - Activity 14
Area Menu
C. Circle
R. Rectangle
S. Square
T. Triangle
X. eXit
Select CRSTX only
methods:
main - calls area method
area - displays the selection, accepts CRSTX input, impelements recursion
circle - calls method input for the radius, compute and display the area of the circle
rectangle - calls method input for the length and width, compute and display the area of the rectangle
square - calls method input for the side, compute and display the area of the square
triangle - calls method input for the base and height, compute and display the area of the triangle
input - validates the input value of radius, length, width, side, base, and height where the allowed value is any positive number, implements recursion for invalid value
C. Circle
R. Rectangle
S. Square
T. Triangle
X. eXit
Select CRSTX only
methods:
main - calls area method
area - displays the selection, accepts CRSTX input, impelements recursion
circle - calls method input for the radius, compute and display the area of the circle
rectangle - calls method input for the length and width, compute and display the area of the rectangle
square - calls method input for the side, compute and display the area of the square
triangle - calls method input for the base and height, compute and display the area of the triangle
input - validates the input value of radius, length, width, side, base, and height where the allowed value is any positive number, implements recursion for invalid value
Lunes, Enero 7, 2019
Methods - Activities 7-9
7. Select an Area to Perform
Menu
C. Circle
R. Rectangle
S. Square
T. Triangle
X. eXit
Select [CRSTX] only:
Methods: main - calls method menu
menu - displays the menu selection
allows the user to select an area to perform and redisplays the menu until 'X' is selected, other characters would result to 'Invalid choice' output
circle - input the value of the radius
compute the area of the circle
rectangle - input the value of the length and width
compute the area of the rectangle
square - input the value of the side
compute the area of the square
triangle - input the value of the base and height
compute the area of the triangle
result - displays the result of the selected area
Test Data: Area input value output
C r = 5 78.54
R l = 5, w = 3 15.0
S s = 4 16.0
T b = 3, h = 5 7.5
A Invalid choice
Additional Requirement: Once the program is running, implement a looping process for methods circle, rectangle, square, and triangle to accept positive number/s only.
8. Implement a looping process for the inputs on prelim exam score and 5 quiz scores where the allowed entries range from 50 to 100 only.
Methods: main - input student name, prelim exam score
quiz - input 5 quiz scores
average quiz - compute the average quiz
prelim grade - compute the prelim grade
remarks - based on the prelim grade, determine if 'Passed' or 'Failed'
output - display student name, prelim grade, remarks
where: prelim grade = 60% of the average quiz + 40% of prelim exam score
table: prelim grade remarks
0 - 74 Failed
75 - 100 Passed
Test Data 1: Input: name = Ana
prelim exam score = 100
5 quiz scores = 100, 100, 100, 100, 100
Output: name = Ana
prelim grade = 100.0
remarks = Passed
Test Data 2: Input: name = Ben
prelim exam score = 50
5 quiz scores = 50, 50, 50, 50, 50
Output: name = Ben
prelim grade = 50.0
remarks = Failed
Test Data 3: Input: name = Cherry
prelim exam score = 90
5 quiz scores = 90, 100, 50, 70, 80
Output: name = Cherry
prelim grade = 82.8
remarks = Passed
Test Data 4: Input: name = Dan
prelim exam score = 0 -> invalid, allowed value is 50-100 only
prelim exam score = 500 -> invalid, allowed value is 50-100 only
prelim exam score = 80
5 quiz scores = 80
100
0 -> invalid, allowed value is 50-100 only
50
100
80
Output: name = Dan
prelim grade = 81.2
remarks = Passed
Additional Requirement: Ask the user to TRY AGAIN [Y/N]
9. input: employee name, employee code, number of days worked
*allowed employee code is CPR, other characters are invalid
*number of days work is from 0-13 only, all other numbers are invalid
output: employee name, gross pay, sss, tax, total deduction, net pay
where: gross pay = rate per day * number of days worked
sss = 10% of gross pay
tax = 15% of gross pay
total deduction = sss + tax
net pay = gross pay - total deduction
table: employee code rate per day
C 500.0
P 520.0
R 550.0
methods: main - calls method employee
employee - accepts all input requirements
rate - determines the rate per day
gross pay - computes the gross pay
sss - computes the sss
tax - computes the tax
total deduction - computes the total deduction
net pay - computes the net pay
output - displays all output requirements
Test Data1: input: employee name = David
employee code = C
number of days worked = 10
output: employee name = David
gross pay = 5000.0
sss = 500.0
tax = 750.0
total deduction = 1250.0
net pay = 3750.0
Test Data2: input: employee name = Emma
employee code = P
number of days worked = 12
output: employee name = Emma
gross pay = 6240.0
sss = 624.0
tax = 936.0
total deduction = 1560.0
net pay = 4680.0
Test Data3: input: employee name = France
employee code = R
number of days worked = 12
output: employee name = France
gross pay = 6600.0
sss = 660.0
tax = 990.0
total deduction = 1650.0
net pay = 4950.0
Menu
C. Circle
R. Rectangle
S. Square
T. Triangle
X. eXit
Select [CRSTX] only:
Methods: main - calls method menu
menu - displays the menu selection
allows the user to select an area to perform and redisplays the menu until 'X' is selected, other characters would result to 'Invalid choice' output
circle - input the value of the radius
compute the area of the circle
rectangle - input the value of the length and width
compute the area of the rectangle
square - input the value of the side
compute the area of the square
triangle - input the value of the base and height
compute the area of the triangle
result - displays the result of the selected area
Test Data: Area input value output
C r = 5 78.54
R l = 5, w = 3 15.0
S s = 4 16.0
T b = 3, h = 5 7.5
A Invalid choice
Additional Requirement: Once the program is running, implement a looping process for methods circle, rectangle, square, and triangle to accept positive number/s only.
8. Implement a looping process for the inputs on prelim exam score and 5 quiz scores where the allowed entries range from 50 to 100 only.
Methods: main - input student name, prelim exam score
quiz - input 5 quiz scores
average quiz - compute the average quiz
prelim grade - compute the prelim grade
remarks - based on the prelim grade, determine if 'Passed' or 'Failed'
output - display student name, prelim grade, remarks
where: prelim grade = 60% of the average quiz + 40% of prelim exam score
table: prelim grade remarks
0 - 74 Failed
75 - 100 Passed
Test Data 1: Input: name = Ana
prelim exam score = 100
5 quiz scores = 100, 100, 100, 100, 100
Output: name = Ana
prelim grade = 100.0
remarks = Passed
Test Data 2: Input: name = Ben
prelim exam score = 50
5 quiz scores = 50, 50, 50, 50, 50
Output: name = Ben
prelim grade = 50.0
remarks = Failed
prelim exam score = 90
5 quiz scores = 90, 100, 50, 70, 80
Output: name = Cherry
prelim grade = 82.8
remarks = Passed
Test Data 4: Input: name = Dan
prelim exam score = 0 -> invalid, allowed value is 50-100 only
prelim exam score = 500 -> invalid, allowed value is 50-100 only
prelim exam score = 80
5 quiz scores = 80
100
0 -> invalid, allowed value is 50-100 only
50
100
80
Output: name = Dan
prelim grade = 81.2
remarks = Passed
Additional Requirement: Ask the user to TRY AGAIN [Y/N]
9. input: employee name, employee code, number of days worked
*allowed employee code is CPR, other characters are invalid
*number of days work is from 0-13 only, all other numbers are invalid
output: employee name, gross pay, sss, tax, total deduction, net pay
where: gross pay = rate per day * number of days worked
sss = 10% of gross pay
tax = 15% of gross pay
total deduction = sss + tax
net pay = gross pay - total deduction
table: employee code rate per day
C 500.0
P 520.0
R 550.0
methods: main - calls method employee
employee - accepts all input requirements
rate - determines the rate per day
gross pay - computes the gross pay
sss - computes the sss
tax - computes the tax
total deduction - computes the total deduction
net pay - computes the net pay
output - displays all output requirements
Test Data1: input: employee name = David
employee code = C
number of days worked = 10
output: employee name = David
gross pay = 5000.0
sss = 500.0
tax = 750.0
total deduction = 1250.0
net pay = 3750.0
Test Data2: input: employee name = Emma
employee code = P
number of days worked = 12
output: employee name = Emma
gross pay = 6240.0
sss = 624.0
tax = 936.0
total deduction = 1560.0
net pay = 4680.0
Test Data3: input: employee name = France
employee code = R
number of days worked = 12
output: employee name = France
gross pay = 6600.0
sss = 660.0
tax = 990.0
total deduction = 1650.0
net pay = 4950.0
Additional Requirement: Ask the user to TRY AGAIN [Y/N]
Lunes, Disyembre 3, 2018
Methods - Activities 4-6
4. Select an Area to Perform
Menu
C. Circle
R. Rectangle
S. Square
T. Triangle
Select [CRST] only:
Methods: main - calls method menu
menu - displays the menu selection
allows the user to select an area to perform
circle - input the value of the radius
compute the area of the circle
rectangle - input the value of the length and width
compute the area of the rectangle
square - input the value of the side
compute the area of the square
triangle - input the value of the base and height
compute the area of the triangle
result - displays the result of the selected area
Test Data: Area input value output
C r = 5 78.54
R l = 5, w = 3 15.0
S s = 4 16.0
T b = 3, h = 5 7.5
5. Methods: main - input student name, prelim exam score
quiz - input 5 quiz scores
average quiz - compute the average quiz
prelim grade - compute the prelim grade
remarks - based on the prelim grade, determine if 'Passed' or 'Failed'
output - display student name, prelim grade, remarks
where: prelim grade = 60% of the average quiz + 40% of prelim exam score
table: prelim grade remarks
0 - 74 Failed
75 - 100 Passed
Test Data1: Input: name = Ana
prelim exam score = 100
5 quiz scores = 100, 100, 100, 100, 100
Output: name = Ana
prelim grade = 100.0
remarks = Passed
Test Data2: Input: name = Ben
prelim exam score = 50
5 quiz scores = 50, 50, 50, 50, 50
Output: name = Ben
prelim grade = 50.0
remarks = Failed
Test Data3: Input: name = Cherry
prelim exam score = 90
5 quiz scores = 90, 100, 50, 70, 80
Output: name = Cherry
prelim grade = 82.8
remarks = Passed
6. input: employee name, employee code, number of days worked
output: employee name, gross pay, sss, tax, total deduction, net pay
where: gross pay = rate per day * number of days worked
sss = 10% of gross pay
tax = 15% of gross pay
total deduction = sss + tax
net pay = gross pay - total deduction
table: employee code rate per day
C 500.0
P 520.0
R 550.0
methods: main - calls method employee
employee - accepts all input requirements
rate - determines the rate per day
gross pay - computes the gross pay
sss - computes the sss
tax - computes the tax
total deduction - computes the total deduction
net pay - computes the net pay
output - displays all output requirements
Test Data1: input: employee name = David
employee code = C
number of days worked = 10
output: employee name = David
gross pay = 5000.0
sss = 500.0
tax = 750.0
total deduction = 1250.0
net pay = 3750.0
Test Data2: input: employee name = Emma
employee code = P
number of days worked = 12
output: employee name = Emma
gross pay = 6240.0
sss = 624.0
tax = 936.0
total deduction = 1560.0
net pay = 4680.0
Test Data3: input: employee name = France
employee code = R
number of days worked = 12
output: employee name = France
gross pay = 6600.0
sss = 660.0
tax = 990.0
total deduction = 1650.0
Menu
C. Circle
R. Rectangle
S. Square
T. Triangle
Select [CRST] only:
Methods: main - calls method menu
menu - displays the menu selection
allows the user to select an area to perform
circle - input the value of the radius
compute the area of the circle
rectangle - input the value of the length and width
compute the area of the rectangle
square - input the value of the side
compute the area of the square
triangle - input the value of the base and height
compute the area of the triangle
result - displays the result of the selected area
Test Data: Area input value output
C r = 5 78.54
R l = 5, w = 3 15.0
S s = 4 16.0
T b = 3, h = 5 7.5
5. Methods: main - input student name, prelim exam score
quiz - input 5 quiz scores
average quiz - compute the average quiz
prelim grade - compute the prelim grade
remarks - based on the prelim grade, determine if 'Passed' or 'Failed'
output - display student name, prelim grade, remarks
where: prelim grade = 60% of the average quiz + 40% of prelim exam score
table: prelim grade remarks
0 - 74 Failed
75 - 100 Passed
Test Data1: Input: name = Ana
prelim exam score = 100
5 quiz scores = 100, 100, 100, 100, 100
Output: name = Ana
prelim grade = 100.0
remarks = Passed
Test Data2: Input: name = Ben
prelim exam score = 50
5 quiz scores = 50, 50, 50, 50, 50
Output: name = Ben
prelim grade = 50.0
remarks = Failed
prelim exam score = 90
5 quiz scores = 90, 100, 50, 70, 80
Output: name = Cherry
prelim grade = 82.8
remarks = Passed
6. input: employee name, employee code, number of days worked
output: employee name, gross pay, sss, tax, total deduction, net pay
where: gross pay = rate per day * number of days worked
sss = 10% of gross pay
tax = 15% of gross pay
total deduction = sss + tax
net pay = gross pay - total deduction
table: employee code rate per day
C 500.0
P 520.0
R 550.0
methods: main - calls method employee
employee - accepts all input requirements
rate - determines the rate per day
gross pay - computes the gross pay
sss - computes the sss
tax - computes the tax
total deduction - computes the total deduction
net pay - computes the net pay
output - displays all output requirements
Test Data1: input: employee name = David
employee code = C
number of days worked = 10
output: employee name = David
gross pay = 5000.0
sss = 500.0
tax = 750.0
total deduction = 1250.0
net pay = 3750.0
Test Data2: input: employee name = Emma
employee code = P
number of days worked = 12
output: employee name = Emma
gross pay = 6240.0
sss = 624.0
tax = 936.0
total deduction = 1560.0
net pay = 4680.0
Test Data3: input: employee name = France
employee code = R
number of days worked = 12
output: employee name = France
gross pay = 6600.0
sss = 660.0
tax = 990.0
total deduction = 1650.0
net pay = 4950.0
Lunes, Nobyembre 26, 2018
Methods - Activities 1-3
1.
Input two numbers. Get the sum, difference, product, and
quotient of these numbers. Display the
results afterwards.
Methods: main
– input
sum
difference
product
quotient
output
2.
Given: At = 1/2bh
Ac
= πr2 (π=3.1416)
Ar
= lw
As
= s2
Input two numbers, n1 and n2.
Let: n1
= b, r, l, s
n2 = h, w
Methods: main
– input
triangle
circle
rectangle
square
output
Example: input => n1=5, n2=3
output => Area of Triangle is 7.5
Area of Circle is 78.54
Area of Rectangle is 15
Area of Square is 25
3.
Input: Employee
Name
Rate
Per Day
Number
of Days Worked
Where: Gross
Pay = Rate Per Day * Number of Days Worked
SSS
= 10% of Gross Pay
Tax
= 15% of Gross Pay
Total
Deduction = SSS + Tax
Net
Pay = Gross Pay – Total Deduction
Methods: main
– input
gross
pay
sss
tax
total
deduction
net
pay
output
Example: input
=> Name = Ana
Rate
Per Day = 500
Number of Days Worked = 10
output => Gross Pay is 5000
SSS is 500
Tax is 750
Total Deduction is 1250
Net Pay is 3750
Mag-subscribe sa:
Mga Post (Atom)
Activity #17 - One-Dimensional Array
Place all codes under the method main. 1. Input 5 numbers and satisfy the ff: a) sum of all input numbers b) count of odd and ev...