Skip to content

Instantly share code, notes, and snippets.

@golenishchev
Last active January 14, 2016 18:16
Show Gist options
  • Save golenishchev/014cee2af3fb29abb0f8 to your computer and use it in GitHub Desktop.
Save golenishchev/014cee2af3fb29abb0f8 to your computer and use it in GitHub Desktop.
Lesson 15
package computer;
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(double operand1, double operand2) {
return resultValue = operand1 + 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(double numberBeforeSquareRootExtraction) {
this.numberBeforeSquareRootExtraction = numberBeforeSquareRootExtraction;
return squareRootCalcResult = Math.sqrt(numberBeforeSquareRootExtraction);
}
public double getSine(double angleBeforeSineCalc) {
this.angleBeforeSineCalc = angleBeforeSineCalc;
return sineCalcResult = Math.sin(angleBeforeSineCalc);
}
public void runCalculator(boolean computerStatus) throws ComputerAccessException {
this.computerStatus = computerStatus;
if (computerStatus) {
System.out.println("Computer works");
} else {
throw new ComputerAccessException("You can't use this program because computer is off\n");
}
}
/* 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();
try {
myCalc.runCalculator(false);
} catch (ComputerAccessException e) {
System.err.print(e);
}
double resultValue = myCalc.getSum(myCalc.operand1, myCalc.operand2);
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);
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);
try {
dataInput.useLocale(Locale.ENGLISH);
myCalc.operand1 = dataInput.nextDouble();
myCalc.operation = dataInput.next();
myCalc.operand2 = dataInput.nextDouble();
} catch (Exception e) {
dataInput.useLocale(Locale.FRANCE);
myCalc.operand1 = dataInput.nextDouble();
myCalc.operation = dataInput.next();
myCalc.operand2 = dataInput.nextDouble();
}
switch (myCalc.operation) {
case "+": {
myCalc.getSum(myCalc.operand1, myCalc.operand2);
myCalc.printResult(myCalc.resultValue);
}
break;
case "-":
System.out.println(myCalc.operand1 + " - " + myCalc.operand2 + " = " + (myCalc.operand1 - myCalc.operand2));
break;
case "*":
System.out.println(myCalc.operand1 + " * " + myCalc.operand2 + " = " + (myCalc.operand1 * myCalc.operand2));
break;
case "/":
System.out.println(myCalc.operand1 + " / " + myCalc.operand2 + " = " + (myCalc.operand1 / myCalc.operand2));
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.resultValue, myCalc.divResultValue};
myCalc.storeResultInAnArray(twoDimArray);
}
}
package computer;
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;
public Computer() { // default constructor
}
/* 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");
}
}
package computer;
public class ComputerAccessException extends Exception {
// Default constructor
public ComputerAccessException() {
}
// Constructor that accepts a message
public ComputerAccessException(String message) {
super(message);
}
}
package computer;
import java.io.*;
public class FileManager {
public static 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 static 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();
}
}
public static void main(String[] args) throws Exception {
String firstFile = "first.txt";
String secondFile = "second3.txt";
String fileToBeDeleted = "second3.txt";
copyFile(firstFile, secondFile);
deleteFile(fileToBeDeleted);
}
}
package computer;
public class Main {
public static void main(String args[]) {
Computer myComp = new Computer(); // object myComp
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());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment