Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xmaslecture v1 Tim Nolle #4

Open
wants to merge 4 commits into
base: xmaslecturev1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
ProfCalculator/.classpath
.project
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not a got idea to ignore these files. These are needed for others to import the project.

# Compiled class file
*.class

Expand All @@ -21,3 +23,101 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*


# Created by https://www.toptal.com/developers/gitignore/api/java,eclipse
# Edit at https://www.toptal.com/developers/gitignore?templates=java,eclipse

### Eclipse ###
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/*
.loadpath
.recommenders

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# PyDev specific (Python IDE for Eclipse)
*.pydevproject

# CDT-specific (C/C++ Development Tooling)
.cproject

# CDT- autotools
.autotools

# Java annotation processor (APT)
.factorypath

# PDT-specific (PHP Development Tools)
.buildpath

# sbteclipse plugin
.target

# Tern plugin
.tern-project

# TeXlipse plugin
.texlipse

# STS (Spring Tool Suite)
.springBeans

# Code Recommenders
.recommenders/

# Annotation Processing
.apt_generated/
.apt_generated_test/

# Scala IDE specific (Scala & Java development for Eclipse)
.cache-main
.scala_dependencies
.worksheet

# Uncomment this line if you wish to ignore the project description file.
# Typically, this file would be tracked if it contains build/dependency configurations:
#.project

### Eclipse Patch ###
# Spring Boot Tooling
.sts4-cache/

### Java ###
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# End of https://www.toptal.com/developers/gitignore/api/java,eclipse

3 changes: 1 addition & 2 deletions ProfCalculator/.classpath
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-13">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/java-11-openjdk">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/JavaFx"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/JavaFX"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
Expand Down
9 changes: 5 additions & 4 deletions ProfCalculator/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=13
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=13
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=13
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=11
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package de.uulm.sp.swt.profcalculator;

import de.uulm.sp.swt.profcalculator.expressions.Addition;
import de.uulm.sp.swt.profcalculator.expressions.Div;
import de.uulm.sp.swt.profcalculator.expressions.Expression;
import de.uulm.sp.swt.profcalculator.expressions.Multiplication;
import de.uulm.sp.swt.profcalculator.expressions.NecessaryBrackets;
import de.uulm.sp.swt.profcalculator.expressions.Sub;
import de.uulm.sp.swt.profcalculator.expressions.Value;
import javafx.application.Application;
import javafx.event.ActionEvent;
Expand All @@ -28,7 +30,9 @@ public class ProfCalculator extends Application implements EventHandler<ActionEv
private TextField inputField = new TextField();

private Button additionButton = new Button("+");
private Button subtractionButton = new Button("-");
private Button multiplicationButton = new Button("*");
private Button divisionButton = new Button("/");

private Label resultLabel = new Label();

Expand All @@ -37,32 +41,40 @@ public void start(Stage stage) throws Exception {
stage.setTitle("Professorial Calculator");
errorLabel.setTextFill(Color.web("#AA0000"));

VBox layout = new VBox(10, errorLabel, inputField, additionButton, multiplicationButton, resultLabel);
VBox layout = new VBox(10, errorLabel, inputField, additionButton, subtractionButton, multiplicationButton, divisionButton, resultLabel);
layout.setPadding(new Insets(20, 80, 20, 80));
Scene scene = new Scene(layout);

stage.setScene(scene);
stage.show();
additionButton.setOnAction(this);
subtractionButton.setOnAction(this);
multiplicationButton.setOnAction(this);
divisionButton.setOnAction(this);

updateGUI();
}

@Override
public void handle(ActionEvent event) {
try {
int newValue = Integer.parseInt(inputField.getText());
double newValue = Double.parseDouble(inputField.getText());
if (event.getSource() == additionButton) {
expression = new Addition(expression, new Value(newValue));
}
else if (event.getSource() == multiplicationButton) {
} else if (event.getSource() == subtractionButton) {
expression = new Sub(expression, new Value(newValue));
} else if (event.getSource() == multiplicationButton) {
expression = new Multiplication(expression, new Value(newValue));
} else if (event.getSource() == divisionButton) {
expression = new Div(expression, new Value(newValue));
}
expression = new NecessaryBrackets(expression);
updateGUI();
inputField.requestFocus();
} catch (NumberFormatException e) {
errorLabel.setText("\"" + inputField.getText() + "\" is not a valid integer");
}catch (ArithmeticException e) {
errorLabel.setText(e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public String toString(Expression parent) {
return left.toString(this) + " + " + right.toString(this);
}

public int evaluate() {
public double evaluate() {
return left.evaluate() + right.evaluate();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package de.uulm.sp.swt.profcalculator.expressions;

public class Div extends Expression {

public Expression left;
public Expression right;

public Div(Expression left, Expression right) {
this.left = left;
this.right = right;
}

public String toString(Expression parent) {
return left.toString(this) + " / " + right.toString(this);
}

public double evaluate() {
if (right.evaluate() == 0)
throw new ArithmeticException("Division by zero is not allowed!");
return left.evaluate() / right.evaluate();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public abstract class Expression {

public abstract int evaluate();
public abstract double evaluate();

public abstract String toString(Expression parent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public String toString(Expression parent) {
return left.toString(this) + " * " + right.toString(this);
}

public int evaluate() {
public double evaluate() {
return left.evaluate() * right.evaluate();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ public NecessaryBrackets(Expression expression) {
@Override
public String toString(Expression parent) {
String childString = expression.toString(parent);
if (parent instanceof Multiplication && expression instanceof Addition) {
if ((parent instanceof Multiplication && (expression instanceof Addition || expression instanceof Sub)) ||
(parent instanceof Div && (expression instanceof Addition || expression instanceof Sub))) {
childString = "(" + childString + ")";
}
return childString;
}

@Override
public int evaluate() {
public double evaluate() {
return expression.evaluate();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.uulm.sp.swt.profcalculator.expressions;

public class Sub extends Expression {

public Expression left;
public Expression right;

public Sub(Expression left, Expression right) {
this.left = left;
this.right = right;
}

public String toString(Expression parent) {
return left.toString(this) + " - " + right.toString(this);
}

public double evaluate() {
return left.evaluate() - right.evaluate();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

public class Value extends Expression {

Integer value;
Double value;

public Value(int value) {
public Value(double value) {
this.value = value;
}

Expand All @@ -13,8 +13,8 @@ public String toString(Expression parent) {
}

@Override
public int evaluate() {
return value.intValue();
public double evaluate() {
return value;
}

}