OOP 1 - Using STATIC Classes
Firstly, set up a new package called OOP1_Calculator.
Your job is to create 2 new classes in your OOP1_Calculator package. One will be a Java Main Class. The other just a java class.:
OOP1CalculatorMethods
STATIC classes are designed with useful methods but are not ever going to be instantiated. This will be one such static (methods only) class. Your goal is to create 6 methods that do simple calculations. The logic is meant to be easy. The result will be that you will be comfortable with the concepts before we move onto more complex OOP.
Each method must:
Suggestions are:
OOP1CalculatorMain
This will contain the main method. In it you should build a menu for the user that you will add to as the assignment progresses. Start by just setting up a loop that continues until the user says “exit”.
Eventually, the menu should:
Evaluation:
OLD & BAD Instructions are Below
Last year (or earlier this year) you created a simple calculator as an introduction to passing parameters to methods. One version of this old assignment is copied on the next page of this document. Read through the code on the next pages to remind yourself how it was written. You are allowed to copy and paste whatever you want from this original code.
Ignore for 2015/16 - Note: the last 10% of many assignments in this credit is to make a GUI based version of the assignment. For this assignment, I will allow Pop Ups (JOptionPane) to count as GUI.
Your job is to create 2 new classes. One will be a Java Main Class. The other just a java class.:
OOP1CalculatorMain
This will contain the main method. In it you should build a menu for the user that you will add to as the assignment progresses. Start by just setting up a loop that continues until the user says “exit”.
Eventually, the menu should:
OOP1CalculatorMethods
This will be a methods only class. Your goal is to create 5 methods that simplify calculations. Some of the methods described are actually available in some form in the Math class but your job is to recreate them.
Note: No printlns should be used in this class. All methods should RETURN their answer.
Code based on a Grade 11 Simple Calculator Assignment
import java.util.Scanner;
public class OOP1CalculatorMain {
//Global Variables
static Scanner numscan = new Scanner(System.in);
static Scanner wordscan = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Welcome to the calculator.");
String choice;
while (true) {
System.out.println("Please make a choice from the following list.");
System.out.println("Do Simple Math - please type math");
System.out.println("Find C in a Pythagorus Triangle = please type find c");
System.out.println("Find a number raised to the power of another - please type power");
System.out.println("????? - please type ?????");
System.out.println("?????? - please type ????");
System.out.println("To exit - please type exit");
System.out.println("Enter your choice now.");
choice = wordscan.nextLine().toLowerCase();
if(choice.equals("exit")) {
return;
}
if(choice.equals(“math”){
System.out.println("Enter your operator (+, -, *, /)");
String operator = wordscan.nextLine();
System.out.println("Enter first number:");
double num1 = numscan.nextDouble();
System.out.println("Enter second number:");
double num2 = numscan.nextDouble();
//the next line needs to be tweaked to work with OOP
System.out.println("The answer is " + OOP1Calculator.doMath(operator, num1, num2);
} else if (choice.equals(“find c”){
System.out.println("For your triangle, enter the length of A.");
double num1 = numscan.nextDouble();
System.out.println("Now enter the length of B.");
double num2 = numscan.nextDouble();
//the next line needs to be tweaked to work with OOP
System.out.println("The length of c is " + OOP1Calculator.pythagorus(num1, num2);
}
//need to add power() and the other two option
}//End While
}//End Main
}//end OOP1CalculatorMain
public class OOP1Calculator{
public static double doMath(String op, double n1, double n2) {
if(op.equals("+")) {
return n1+n2;
} else if(op.equals("-")) {
return n1-n2;
} else if(op.equals("*")) {
return n1*n2;
} else if(op.equals("/")) {
return n1/n2;
} else {
return 0;//This is for debugging purposes
}
}//End doMath
public static double pythagorus (int a, int b){
int c = 0;
return c;
}
public static int power(int num, int exponent){
int total = 0;
return total;
}
}//End OOP1Calculator Class