Activity Problem: Use Chapter 4 Lesson2 - Classes&Methods Powerpoint. Inside The

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Activity Problem: Use Chapter 4 Lesson2_Classes&Methods powerpoint.

Inside the
powerpoint do the Calculator Practice Exercise, named it Activity_8. The Activity_8 is
still by pair. The pointing system (divide by 10) is on the slide before the slide of the
calculator.
Activity Code:
package activity8_calculator;
import java.util.*;
import static java.lang.Math.*;
public class Activity8_Calculator {

/* First, we've declared some private variables for our methods so it


can be used multiple times by the methods without errors.*/
private static double n1;
private static double n2;
private static int in1;
private static int in2;
private static int option;

/* Next, we've created our menu() using Two Dimensional Array for more
convenient printing of the menu instead of putting spaces between the options
, we've used void for our method. Afterwards, we've created the following
methods with their respective functions inside.*/
public static void menu (){
System.out.println("*********************************************************");
System.out.println("*\t\t\tCALCULATOR\t\t\t*");
System.out.println("*********************************************************");
String [][] Options = {{"[1] Addition\t\t\t", "[2] Subtraction\t\t\t",
"[3] Multiplication\t\t", "[4] Division\t\t\t", "[5] Raise to an Exponent\t"},
Name/s: Date: 3 / 25 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
{"[6] Square Root", "[7] Squared", "[8] a. SIN b. COS c. TAN", "[9] LOG", "[0]
Exit"}};
for(int a = 0; a < 5; a++){
System.out.println(Options[0][a] + Options[1][a]);
}
System.out.println("*********************************************************");
}

public static void input (){


Scanner input = new Scanner(System.in);
System.out.print("Enter the First Number: ");
n1 = input.nextInt(); System.out.println();
System.out.print("Enter the Second Number: ");
n2 = input.nextInt(); System.out.println();
}

public static int addition (){


int Sum = in1 + in2;
System.out.println("The Sum of " +in1 +" and " +in2 +" is " +Sum);
return Sum;
}

public static int subtraction (){


int Dif = in1 - in2;
System.out.println("The Difference of " +in1 +" Subtracted by " +in2 +" is " +Dif);
return Dif;
}

Name/s: Date: 3 / 25 / 2020


Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
public static void multiplication (){
int Prod = in1 * in2;
System.out.println("The Product of " +in1 +" and " +in2 +" is " +Prod);
}

public static void division (){


if(n2 == 0){
System.out.println("Invalid Entry! In Division, the Denominator"
+ " cannot be equal to 0. Please try Again."); System.out.println();
input_for_division();
division();
}
else{
double Quo = n1 / n2;
System.out.println("The Quotient of " +n1 +" Divided by " +n2 +" is " +Quo);
}
}

/* Subsequently, we've created a try_again() method with a return type


of char (character), inside of the method is a switch case that prompt
the entered data, if it is yes or no or neither of the two, if the user
wants to continue the program it will call the main() method to loop back
from the start, this process can be infinite until the user doesn't want
to continue the program.*/
public static char try_again(char t){
switch(t){
Name/s: Date: 3 / 25 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
case 'y':
case 'Y':
branch();

case 'n':
case 'N':{
System.exit(0);
}

default:
System.out.println("Invalid Entry!");
branch();
break;
}
return t;
}

public static int choice (){


Scanner input = new Scanner (System.in);
System.out.print("Enter your Choice: ");
option = Integer.parseInt(input.nextLine()); System.out.println();

if(option > 9){


System.out.print("Invalid Entry! Do you want to Try Again? [y] Yes or [n] No : ");
char ta = input.nextLine().charAt(0);
System.out.println(); Invalid_try_again(ta);
}
Name/s: Date: 3 / 25 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
else if(option < 0){
System.out.print("Invalid Entry! Do you want to Try Again? [y] Yes or [n] No : ");
char ta = input.nextLine().charAt(0);
System.out.println(); Invalid_try_again(ta);
}
else{
switch(option){

case 0:
System.out.print("Are you sure you want to exit? [y] yes or [n] no : ");
char ex = input.nextLine().charAt(0);
System.out.println(); Confirmation_exit (ex);

case 1:
inputInt();
addition();
System.out.println(); break;

case 2:
inputInt();
subtraction();
System.out.println(); break;

case 3:
inputInt();
multiplication();
System.out.println(); break;
Name/s: Date: 3 / 25 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
case 4:
input_for_division();
division();
System.out.println(); break;

case 5:
input_for_Exponent();
raise_to_an_exponent();
System.out.println(); break;

case 6:
input_for_SingleInt();
square_root();
System.out.println(); break;

case 7:
inputSingleInt();
squared();
System.out.println(); break;

case 8:
System.out.print("Trigonometric Function ? a | b | c : ");
char tri = input.nextLine().charAt(0); System.out.println();
switch(tri){
case 'a':
case 'A':
Name/s: Date: 3 / 25 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
input_for_SingleInt();
sine();
System.out.println(); break;

case 'b':
case 'B':
input_for_SingleInt();
cosine();
System.out.println(); break;

case 'c':
case 'C':
input_for_SingleInt();
tangent();
System.out.println(); break;

default:
System.out.print("Invalid Entry! Do you want to Try Again? [y] Yes or
[n] No : ");
char ta = input.nextLine().charAt(0);
System.out.println(); Invalid_try_again(ta); break;
} break;

case 9:
input_for_SingleInt();
logarithm();
System.out.println(); break;

Name/s: Date: 3 / 25 / 2020


Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
default:
System.out.print("Invalid Entry! Do you want to Try Again? [y] Yes or [n] No
: ");
char ta = input.nextLine().charAt(0);
System.out.println(); Invalid_try_again(ta); break;
}
}
return option;
}

/* These are the Remaining Methods for the Program, inside of the methods
are the functions that were based on "Math Class". */
public static void square_root (){
double Sqrt = sqrt(n1);
System.out.println("The Square Root of " +n1 +" is " +Sqrt);
}

public static void squared (){


int Sqrd = in1 * in1;
System.out.println(in1 +" Squared is equal to " +Sqrd);
}

/* For the Trigonometric Functions, the Trigo Value can be only printed on
Radian Form, we've try different method for printing the Degree Form of the
Trigo Value but we can't come up with any possible ways, we've try to convert
the Radian Value of the Trigo. Function using toDegrees(double angrad) but

Name/s: Date: 3 / 25 / 2020


Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
the answer is incorrect. */
public static void sine (){
double Sin = sin(n1);
System.out.println("The SINE Value in Radian Mode is " +Sin);
}

public static void cosine (){


double Cos = cos(n1);
System.out.println("The COSINE Value in Radian Mode is " +Cos);
}

public static void tangent (){


double Tan = tan(n1);
System.out.println("The TANGENT Value in Radian Mode is " +Tan);
}

public static void logarithm (){


double Log = log10(n1);
System.out.println("The LOGARITHMIC Value is " +Log);
}

/* The following methods are for the methods that needs unique input methods
such as the Trigonometric Functions, Square Root, and Logarithm that only
needs single number. */
public static void inputInt (){
Scanner input = new Scanner(System.in);
System.out.print("Enter the First Number: ");
Name/s: Date: 3 / 25 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
in1 = input.nextInt(); System.out.println();
System.out.print("Enter the Second Number: ");
in2 = input.nextInt(); System.out.println();
}

public static void input_for_division (){


Scanner input = new Scanner(System.in);
System.out.print("Enter the Divisor Number: ");
n1 = input.nextInt(); System.out.println();
System.out.print("Enter the Denominator Number: ");
n2 = input.nextInt(); System.out.println();
}

public static void input_for_Exponent (){


Scanner input = new Scanner(System.in);
System.out.print("Enter a Number: ");
n1 = input.nextInt(); System.out.println();
System.out.print("Enter an Exponent: ");
n2 = input.nextInt(); System.out.println();
}

public static void input_for_SingleInt (){


Scanner input = new Scanner(System.in);
System.out.print("Enter a Number: ");
n1 = input.nextInt(); System.out.println();
}

Name/s: Date: 3 / 25 / 2020


Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
public static void inputSingleInt (){
Scanner input = new Scanner(System.in);
System.out.print("Enter a Number: ");
in1 = input.nextInt(); System.out.println();
}

public static void raise_to_an_exponent (){


double Exp = pow(n1, n2);
System.out.println("The Result of " +n1 +" raised to " +n2 +" is " +Exp);
}

/* These remaining methods are just extra methods that we made for the
users, in case the user entered an invalid character or for confirmation of
exiting the program. */
public static char Invalid_try_again (char ta){
switch(ta){
case 'y':
case 'Y':
branch();

case 'n':
case 'N':{
System.exit(0);
}

default:
System.out.println("Invalid Entry!");
Name/s: Date: 3 / 25 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
branch();
break;
}
return ta;
}

public static char Confirmation_exit (char ex){


switch(ex){
case 'y':
case 'Y':
System.exit(0);

case 'n':
case 'N':
branch();

default:
System.out.println("Invalid Entry!");
branch(); break;
}
return ex;
}

/* This method is a replica of the main method, because it is not possible


to call the main method by the another methods if they don't have the same
return value. To make that possible, we've created a replica of the main()
so that in the try_again method, instead of the main() method, this method
Name/s: Date: 3 / 25 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
is going to be called that contains a similar function to the main(). */
public static void branch(){
Scanner input = new Scanner (System.in);
menu();
choice();

System.out.print("Do you want to Try Again? [y] Yes or [n] No : ");


char t = input.nextLine().charAt(0);
System.out.println(); try_again (t);
}

/* Inside the main() method, we've used menu(), choice(), and try_again
which are essential for the program. */
public static void main(String[] args){
Scanner input = new Scanner (System.in);
menu();
choice();

System.out.print("Do you want to Try Again? [y] Yes or [n] No : ");


char t = input.nextLine().charAt(0);
System.out.println(); try_again (t);
}
}

Name/s: Date: 3 / 25 / 2020


Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
Program:

Name/s: Date: 3 / 25 / 2020


Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
Name/s: Date: 3 / 25 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
Name/s: Date: 3 / 25 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
Name/s: Date: 3 / 25 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
Name/s: Date: 3 / 25 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
Name/s: Date: 3 / 25 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
Output:

Name/s: Date: 3 / 25 / 2020


Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
Name/s: Date: 3 / 25 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
Name/s: Date: 3 / 25 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres

You might also like