Last active
January 31, 2016 19:47
-
-
Save golenishchev/c0a3701f3f91cafe0cd9 to your computer and use it in GitHub Desktop.
java.util.InputMismatchException
This file contains 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
package com.example.javalessons; | |
import java.util.Locale; | |
import java.util.Scanner; | |
public class Calculator { | |
private double resultValue; | |
private double divResultValue; | |
private double numberBeforeSquareRootExtraction; | |
private double squareRootCalcResult; | |
private double angleBeforeSineCalc; | |
private double sineCalcResult; | |
private double operand1; | |
private String operation; | |
private double operand2; | |
// private boolean computerStatus; | |
public double getSum() { | |
return resultValue = operand1 + operand2; | |
} | |
public double getResultValue() { | |
return resultValue; | |
} | |
public void setResultValue(double resultValue) { | |
this.resultValue = resultValue; | |
} | |
public double getDivResultValue() { | |
return divResultValue; | |
} | |
public void setDivResultValue(double divResultValue) { | |
this.divResultValue = divResultValue; | |
} | |
public double getNumberBeforeSquareRootExtraction() { | |
return numberBeforeSquareRootExtraction; | |
} | |
public void setNumberBeforeSquareRootExtraction(double numberBeforeSquareRootExtraction) { | |
this.numberBeforeSquareRootExtraction = numberBeforeSquareRootExtraction; | |
} | |
public double getSquareRootCalcResult() { | |
return squareRootCalcResult; | |
} | |
public void setSquareRootCalcResult(double squareRootCalcResult) { | |
this.squareRootCalcResult = squareRootCalcResult; | |
} | |
public double getAngleBeforeSineCalc() { | |
return angleBeforeSineCalc; | |
} | |
public void setAngleBeforeSineCalc(double angleBeforeSineCalc) { | |
this.angleBeforeSineCalc = angleBeforeSineCalc; | |
} | |
public double getSineCalcResult() { | |
return sineCalcResult; | |
} | |
public void setSineCalcResult(double sineCalcResult) { | |
this.sineCalcResult = sineCalcResult; | |
} | |
public double getOperand1() { | |
return operand1; | |
} | |
public void setOperand1(double operand1) { | |
this.operand1 = operand1; | |
} | |
public String getOperation() { | |
return operation; | |
} | |
public void setOperation(String operation) { | |
this.operation = operation; | |
} | |
public double getOperand2() { | |
return operand2; | |
} | |
public void setOperand2(double operand2) { | |
this.operand2 = operand2; | |
} | |
public void printResult(double resultValue) { | |
System.out.println("Result value: " + resultValue); | |
} | |
public double getDiv(double firstNumber, double secondNumber) { | |
return divResultValue = firstNumber / secondNumber; | |
} | |
/* BEGIN methods of Math class */ | |
public double getSqrt() { | |
return squareRootCalcResult = Math.sqrt(numberBeforeSquareRootExtraction); | |
} | |
public void setSqrt(double numberBeforeSquareRootExtraction) { | |
this.numberBeforeSquareRootExtraction = 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(); | |
} | |
} | |
} | |
This file contains 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
package com.example.javalessons; | |
import java.util.Scanner; | |
public class Computer implements java.io.Serializable { // Creating class Computer | |
private int powerSupply; // fields | |
private float processor; | |
private int videoCard; | |
private String motherBoard; | |
boolean computerStatusOn; | |
public Computer() {// default constructor | |
powerSupply = 450; | |
processor = 2.4f; | |
videoCard = 11; | |
motherBoard = "Asus P5K"; | |
} | |
/* getter and setter methods | |
BEGIN powerSupply */ | |
public int getPowerSupply() { | |
return powerSupply; | |
} | |
public void setPowerSupply(int powerSupply) { | |
this.powerSupply = powerSupply; | |
} | |
// BEGIN processor | |
public float getProcessor() { | |
return processor; | |
} | |
public void setProcessor(float processor) { | |
this.processor = processor; | |
} | |
// BEGIN videoCard | |
public int getVideoCard() { | |
return videoCard; | |
} | |
public void setVideoCard(int videoCard) { | |
this.videoCard = videoCard; | |
} | |
// BEGIN motherBoard | |
public String getMotherBoard() { | |
return motherBoard; | |
} | |
public void setMotherBoard(String motherBoard) { | |
this.motherBoard = motherBoard; | |
} | |
public void turnOnComputer() { | |
System.out.println("Type 1 and press Enter to turn me on"); | |
System.out.println("Type 2 if don't wanna please me"); | |
Scanner sc = new Scanner(System.in); | |
switch (sc.nextInt()) { | |
case 1: | |
System.out.println("It's alive!"); | |
chooseWhatToDoNext(); | |
break; | |
case 2: | |
System.out.println("Give me the power and nobody gets hurt! Please!"); | |
System.out.println("Computer power not turned on. Do you know what \"eien\" means in Japanese?\n"); // it means "eternity" | |
turnOnComputer(); | |
break; | |
default: | |
System.out.println("You can choose only between 1 and 2"); | |
turnOnComputer(); | |
break; | |
} | |
} | |
public void chooseWhatToDoNext() { | |
System.out.println("What do you want to do next?"); | |
System.out.println("Type 1 to shut down. Type 2 to install operating system"); | |
Scanner sc = new Scanner(System.in); | |
switch (sc.nextInt()) { | |
case 1: | |
turnOffComputer(); | |
break; | |
case 2: | |
chooseOSToInstall(); | |
break; | |
default: | |
System.out.println("You can choose only between 1 and 2"); | |
chooseWhatToDoNext(); | |
break; | |
} | |
} | |
public void turnOffComputer() { | |
System.out.println("Shutting down. Done."); | |
} | |
public void chooseOSToInstall() { | |
System.out.println("What OS do you want to install?"); | |
System.out.println("\t 1 Apple OS X \n\t 2 FreeBSD \n\t 3 GNU/Linux \n\t 4 Windows \n\t 5 To go back"); | |
Scanner sc = new Scanner(System.in); | |
switch (sc.nextInt()) { | |
case 1: | |
case 2: | |
case 3: | |
case 4: | |
installOS(); | |
break; | |
case 5: | |
chooseWhatToDoNext(); | |
break; | |
case 6: | |
default: | |
chooseOSToInstall(); | |
break; | |
} | |
} | |
public void installOS() { | |
System.out.println("Installed"); | |
System.out.println("Do you want to boot OS and install some software?"); | |
System.out.println("Type 1 to boot OS. Type 2 to shut down."); | |
Scanner sc = new Scanner(System.in); | |
switch (sc.nextInt()) { | |
case 1: | |
bootOS(); | |
break; | |
case 2: | |
turnOffComputer(); | |
break; | |
} | |
} | |
public void bootOS() { | |
System.out.println("Booted"); | |
System.out.println("You can install Calculator and FileManager."); | |
System.out.println("\t 1 Install both; \n\t 2 Install Calculator \n\t 3 Install FileManager"); | |
Scanner sc = new Scanner(System.in); | |
switch (sc.nextInt()) { | |
case 1: | |
installCalculator(); | |
installFileManager(); | |
break; | |
case 2: | |
installCalculator(); | |
break; | |
case 3: | |
installFileManager(); | |
break; | |
case 4: | |
turnOffComputer(); | |
break; | |
default: | |
System.out.println("You can choose only between 1 and 2"); | |
bootOS(); | |
break; | |
} | |
} | |
public void installCalculator() { | |
System.out.println("Calculator installed"); | |
} | |
public void installFileManager() { | |
System.out.println("FileManager installed"); | |
} | |
public Calculator runCalculator() throws ComputerAccessException { | |
this.computerStatusOn = computerStatusOn; | |
if (computerStatusOn) { | |
System.out.println("Computer is On and Calculator can run"); | |
} else { | |
throw new ComputerAccessException("You can't use this program because computer is off\n"); | |
} | |
return null; | |
} | |
} |
This file contains 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
package com.example.javalessons; | |
public class ComputerAccessException extends Exception { | |
// Default constructor | |
public ComputerAccessException() { | |
} | |
// Constructor that accepts a message | |
public ComputerAccessException(String message) { | |
super(message); | |
} | |
} |
This file contains 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
package com.example.javalessons; | |
import java.io.*; | |
public class FileManager { | |
public void copyFile(String firstFile, String secondFile) throws Exception { | |
try ( | |
BufferedReader br = new BufferedReader(new FileReader(firstFile)); | |
PrintWriter pw = new PrintWriter(new FileWriter(secondFile)) | |
) { | |
int line; | |
while ((line = br.read()) != -1) { | |
pw.write(line); | |
} | |
System.out.println("All done"); | |
} catch (FileNotFoundException e) { | |
System.out.println(firstFile + " not found"); | |
} | |
} | |
public void deleteFile(String fileToBeDeleted) { | |
try { | |
File file = new File(fileToBeDeleted); | |
if (file.delete()) { | |
System.out.println(file.getName() + " is deleted"); | |
} else { | |
System.out.println("deletion failed"); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains 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
package com.example.javalessons; | |
import java.util.Locale; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String args[]) throws Exception { | |
Computer myComp = new Computer(); // object myComp | |
myComp.computerStatusOn = true; | |
myComp.setPowerSupply(450); | |
myComp.setProcessor(2.4f); | |
myComp.setVideoCard(11); | |
myComp.setMotherBoard("Asus P5K"); | |
myComp.turnOnComputer(); | |
System.out.println(myComp.getPowerSupply() + "\n" + myComp.getProcessor() | |
+ "\n" + myComp.getVideoCard() + "\n" + myComp.getMotherBoard()); | |
FileManager myFileManager = new FileManager(); | |
String firstFile = "first.txt"; | |
String secondFile = "second3.txt"; | |
String fileToBeDeleted = "second3.txt"; | |
myFileManager.copyFile(firstFile, secondFile); | |
myFileManager.deleteFile(fileToBeDeleted); | |
Calculator myCalc = new Calculator(); | |
try { | |
myCalc = myComp.runCalculator(); | |
} catch (ComputerAccessException e) { | |
e.printStackTrace(); | |
} | |
// myCalc.setOperand1(30.0); | |
// myCalc.setOperand2(30.0); | |
// DELETE myCalc.setResultValue(myCalc.getSum()); | |
// myCalc.getResultValue(); | |
// myCalc.resultValue = myCalc.getSum(myCalc.operand1, myCalc.operand2); | |
// DELETE myCalc.printResult(myCalc.getResultValue()); | |
// DELETE myCalc.getDiv(10, 2); | |
// DELETE myCalc.getSqrt(100.0); | |
// myCalc.setSqrt(100.0); | |
//DELETE myCalc.getSine(30.0); | |
// int[] array = {12, 1, 5, -11, 7, 13, -1, -5}; | |
// myCalc.findMaxInAnArray(array); | |
// System.out.println("The square root of \"" + myCalc.getNumberBeforeSquareRootExtraction() + "\" is: " + myCalc.getSqrt()); | |
// System.out.println("The sine of \"" + myCalc.getAngleBeforeSineCalc() + "\" is: " + myCalc.getSineCalcResult()); | |
System.out.println("\nType two numbers with symbol betwin them. For example 10 + 5. You can use: + - * /"); | |
Scanner dataInput = new Scanner(System.in); | |
try { | |
dataInput.useLocale(Locale.ENGLISH); | |
myCalc.setOperand1(dataInput.nextDouble()); | |
// myCalc.operand1 = dataInput.nextDouble(); | |
//myCalc.operation = dataInput.next(); | |
myCalc.setOperation(dataInput.next()); | |
myCalc.setOperand2(dataInput.nextDouble()); | |
//myCalc.operand2 = dataInput.nextDouble(); | |
} catch (Exception e) { | |
dataInput.useLocale(Locale.FRANCE); | |
// myCalc.operand1 = dataInput.nextDouble(); | |
// myCalc.operation = dataInput.next(); | |
// myCalc.operand2 = dataInput.nextDouble(); | |
myCalc.setOperand1(dataInput.nextDouble()); | |
myCalc.setOperation(dataInput.next()); | |
myCalc.setOperand2(dataInput.nextDouble()); | |
} | |
switch (myCalc.getOperation()) { | |
case "+": | |
myCalc.getSum(); | |
myCalc.printResult(myCalc.getResultValue()); | |
break; | |
case "-": | |
System.out.println(myCalc.getOperand1() + " - " + myCalc.getOperand2() + " = " + (myCalc.getOperand1() - myCalc.getOperand2())); | |
break; | |
case "*": | |
System.out.println(myCalc.getOperand1() + " * " + myCalc.getOperand2() + " = " + (myCalc.getOperand1() * myCalc.getOperand2())); | |
break; | |
case "/": | |
System.out.println(myCalc.getOperand1() + " / " + myCalc.getOperand2() + " = " + (myCalc.getOperand1() / myCalc.getOperand2())); | |
break; | |
default: | |
System.out.println("You have a typo, try again"); | |
break; | |
} | |
Object[][] twoDimArray = new Object[2][2]; | |
twoDimArray[0] = new String[]{"sum", "division"}; | |
twoDimArray[1] = new Double[]{myCalc.getResultValue(), myCalc.getDivResultValue()}; | |
myCalc.storeResultInAnArray(twoDimArray); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment