Created
December 7, 2015 20:51
-
-
Save golenishchev/0d6784429a7377b9f3da to your computer and use it in GitHub Desktop.
Lesson 11. Switch, case, input from console
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
class Calculator { | |
private int firstNumber; | |
private int secondNumber; | |
private int resultValue; | |
private int divResultValue; | |
private int array; | |
private int twoDimArray; | |
private double wowSoSquare; | |
private double numberBeforeSquareRootExtraction; | |
private double squareRootCalcResult; | |
private double angleBeforeSineCalc; | |
private double sineCalcResult; | |
public int getSum(int firstNumber, int secondNumber) { | |
return resultValue = firstNumber + secondNumber; | |
} | |
public void printResult(int resultValue) { | |
System.out.println("Result value: " + resultValue); | |
} | |
public int getDiv(int firstNumber, int secondNumber) { | |
return divResultValue = firstNumber / secondNumber; | |
} | |
public void printDivResult(int divResultValue) { | |
System.out.println("Division result value: " + divResultValue); | |
} | |
/* BEGIN methods of Math class */ | |
public double getSqrt(double numberBeforeSquareRootExtraction) { | |
this.numberBeforeSquareRootExtraction = numberBeforeSquareRootExtraction; | |
return squareRootCalcResult = Math.sqrt(numberBeforeSquareRootExtraction); | |
} | |
public double getSine(double angleBeforeSineCalc) { | |
this.angleBeforeSineCalc = angleBeforeSineCalc; | |
return sineCalcResult = Math.sin(angleBeforeSineCalc); | |
} | |
/* Array */ | |
public void findMaxInAnArray(int[] array) { | |
int max = array[0]; | |
for(int i = 0; i < array.length; i++){ | |
if(max < array[i]) | |
max = array[i]; | |
} | |
System.out.println("Maximum in an array: " + max); | |
} | |
public void storeResultInAnArray(Object[][] twoDimArray) { | |
for(int row = 0; row < twoDimArray.length; row++) { | |
for(int column = 0; column < twoDimArray.length; column++) { | |
System.out.print(twoDimArray[row][column] + "\t"); | |
} | |
System.out.println(); | |
} | |
} | |
public static void main(String[] args) { | |
Calculator myCalc = new Calculator(); | |
int resultValue = myCalc.getSum(10, 5); | |
myCalc.printResult(resultValue); | |
myCalc.getDiv(10,2); | |
myCalc.getSqrt(100.0); | |
myCalc.getSine(30.0); | |
int [] array = {12,1,5,-11,7,13,-1,-5}; | |
myCalc.findMaxInAnArray(array); | |
Object[][] twoDimArray = new Object[2][2]; | |
twoDimArray[0] = new String[]{"sum", "division"}; | |
twoDimArray[1] = new Integer[]{myCalc.resultValue, myCalc.divResultValue}; | |
myCalc.storeResultInAnArray(twoDimArray); | |
System.out.println("The square root of \"" + myCalc.numberBeforeSquareRootExtraction + "\" is: " + myCalc.squareRootCalcResult); | |
System.out.println("The sine of \"" + myCalc.angleBeforeSineCalc + "\" is: " + myCalc.sineCalcResult); | |
System.out.println("\nType two numbers with symbol betwin them. For example 10 + 5. You can use: + - * /"); | |
Scanner dataInput = new Scanner(System.in); | |
double operand1 = dataInput.nextDouble(); | |
String operation = dataInput.next(); | |
double operand2 = dataInput.nextDouble(); | |
switch (operation) { | |
case "+": System.out.println(operand1 + " + " + operand2 + " = " + (operand1 + operand2)); | |
break; | |
case "-": System.out.println(operand1 + " - " + operand2 + " = " + (operand1 - operand2)); | |
break; | |
case "*": System.out.println(operand1 + " * " + operand2 + " = " + (operand1 * operand2)); | |
break; | |
case "/": System.out.println(operand1 + " / " + operand2 + " = " + (operand1 / operand2)); | |
break; | |
default: System.out.println("You have a typo, try again"); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment