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
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
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...