-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marc Hein
committed
Nov 28, 2015
1 parent
be42a88
commit fbdc0ee
Showing
4 changed files
with
344 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package ppi; | ||
|
||
public class Number { | ||
|
||
private static int firstDot(String s) { | ||
int first = s.indexOf(","); | ||
int second = s.indexOf("."); | ||
|
||
if(first < second) { | ||
return second; | ||
} else { | ||
return first; | ||
} | ||
} | ||
|
||
private static int howManyDots(String s) { | ||
int dots = 0; | ||
for(int i = 0; i < s.length(); i++) { | ||
if(isDot(s.charAt(i))) { | ||
dots++; | ||
} | ||
} | ||
return dots; | ||
} | ||
|
||
private static int howManyNumbers(String s) { | ||
int numbers = 0; | ||
for(int i = 0; i < s.length(); i++) { | ||
if(isNumber(s.charAt(i))) { | ||
numbers++; | ||
} | ||
} | ||
return numbers; | ||
} | ||
|
||
private static boolean isDot(char c) { | ||
return c == '.'; | ||
} | ||
|
||
private static boolean isNumber(char c) { | ||
return c >= '0' && c <= '9'; | ||
} | ||
|
||
private static String removeNotFirstDot(String s) { | ||
String front = s.substring(0, firstDot(s)+1); | ||
String sub = s.substring(firstDot(s)+1); | ||
StringBuffer sb = new StringBuffer(sub); | ||
for(int i = 0; i < sb.length(); i++) { | ||
if(isDot(sb.charAt(i)) || !isNumber(sb.charAt(i))) { | ||
sb.deleteCharAt(i); | ||
} | ||
} | ||
return front + sb.toString(); | ||
} | ||
|
||
private static String removeAllDots(String s) { | ||
StringBuffer sb = new StringBuffer(s); | ||
int length = sb.length(); | ||
for(int i = 0; i < length; i++) { | ||
if(isDot(sb.charAt(i))) { | ||
sb.deleteCharAt(i); | ||
length--; | ||
i--; | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private static String addLeadingZero(String s) { | ||
if(firstDot(s) == 0) | ||
return "0" + s; | ||
else | ||
return s; | ||
} | ||
|
||
private static String addNumber(String s) { | ||
if(howManyNumbers(s) == 0) | ||
return "0"; | ||
else | ||
return s; | ||
} | ||
|
||
private static String format(String s) { | ||
s = s.replaceAll(",", "."); | ||
s = s.replaceAll("[^.;0-9]*", ""); | ||
s = removeNotFirstDot(s); | ||
s = addNumber(s); | ||
return s; | ||
} | ||
|
||
public static String formatLikeInt(String s) { | ||
s = format(s); | ||
if(firstDot(s) != -1) { | ||
s = s.substring(0, firstDot(s)+1); | ||
} | ||
s = removeAllDots(s); | ||
return s; | ||
} | ||
|
||
public static String formatLikeDouble(String s) { | ||
s = format(s); | ||
s = addLeadingZero(s); | ||
while (howManyDots(s) > 1) { | ||
s = removeNotFirstDot(s); | ||
} | ||
return s; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package ppi; | ||
|
||
import ppi.Number; | ||
|
||
public class PPI { | ||
|
||
private double x, y, z; | ||
private String width, height, size; | ||
public boolean debug = false; | ||
|
||
|
||
public PPI(String breite, String hoehe, String groesse){ | ||
|
||
width = String.valueOf(removeAllNonNumber(String.valueOf(breite), "int")); | ||
height = String.valueOf(removeAllNonNumber(String.valueOf(hoehe), "int")); | ||
size = String.valueOf(removeAllNonNumber(String.valueOf(groesse), "double")); | ||
if("0".equals(size)) { | ||
size = "1"; | ||
} | ||
|
||
if(size.indexOf(',') != -1) { | ||
StringBuffer buf = new StringBuffer(size); | ||
buf.setCharAt(buf.indexOf(","), '.'); | ||
size = buf.toString(); | ||
} | ||
try { | ||
x = Double.parseDouble(width); | ||
} catch (NumberFormatException n) { | ||
System.out.println(n.getMessage()); | ||
x = 0; | ||
} | ||
try { | ||
y = Double.parseDouble(height); | ||
if(y == 9000) | ||
debug = true; | ||
} catch (NumberFormatException n) { | ||
System.out.println(n.getMessage()); | ||
y = 0; | ||
} | ||
try { | ||
z = Double.parseDouble(size); | ||
} catch (NumberFormatException n){ | ||
System.out.println(n.getMessage()); | ||
z = 1; | ||
} | ||
} | ||
|
||
public double berechnen() throws IllegalArgumentException { | ||
|
||
if(x <= 0) { | ||
if(x < 1 || width == null) { | ||
throw new IllegalArgumentException("Die Breite kann nicht kleiner als 1 sein."); | ||
} else { | ||
x = x * (-1); | ||
x = Double.parseDouble(x + ""); | ||
x = Math.abs(x); | ||
} | ||
} | ||
|
||
if(y <= 0) { | ||
if(y < 1 || height == null) { | ||
throw new IllegalArgumentException("Die Höhe kann nicht kleiner als 1 sein."); | ||
} else { | ||
y = y * (-1); | ||
y = Double.parseDouble(y + ""); | ||
y = Math.abs(y); | ||
} | ||
} | ||
|
||
if(z <= 0) { | ||
if(z <= 0) { | ||
throw new ArithmeticException("Die Größe des Displays darf nicht 0 sein."); | ||
} else { | ||
z = z * (-1); | ||
z = Double.parseDouble(z + ""); | ||
z = Math.abs(z); | ||
} | ||
} | ||
if(debug) | ||
System.out.println("x = " + x + "\n" + "y = " + y + "\n" + "z = " + z); | ||
|
||
return (double)Math.round(Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) / z * 100000) / 100000; | ||
} | ||
|
||
|
||
public static double removeAllNonNumber(String str, String type) { | ||
try { | ||
if("int".equals(type)) { | ||
return Double.parseDouble(Number.formatLikeInt(str)); | ||
} else { | ||
return Double.parseDouble(Number.formatLikeDouble(str)); | ||
} | ||
|
||
} catch (NumberFormatException n) { | ||
return 0; | ||
} | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package ppi; | ||
|
||
import java.awt.*; | ||
import java.awt.event.*; | ||
import javax.swing.JOptionPane; | ||
|
||
@SuppressWarnings("serial") | ||
public class PPICalculator extends Frame implements ActionListener, WindowListener, FocusListener { | ||
|
||
private Label breite_l, hoehe_l, groesse_l, ergebnis_l; | ||
private TextField breite_f, hoehe_f, groesse_f; | ||
private TextField ergebnis_f; | ||
private Button berechnen; | ||
private Frame frame; | ||
|
||
public PPICalculator() { | ||
frame = new Frame(); | ||
frame.setLayout(new FlowLayout()); | ||
|
||
breite_l = new Label("Breite des Displays (in px):"); | ||
frame.add(breite_l); | ||
|
||
breite_f = new TextField("0", 15); | ||
breite_f.setEditable(true); | ||
breite_f.addFocusListener(this); | ||
frame.add(breite_f); | ||
|
||
hoehe_l = new Label("Höhe des Displays (in px):"); | ||
frame.add(hoehe_l); | ||
|
||
hoehe_f = new TextField("0", 15); | ||
hoehe_f.setEditable(true); | ||
hoehe_f.addFocusListener(this); | ||
frame.add(hoehe_f); | ||
|
||
groesse_l = new Label("Größe des Displays (in Zoll):"); | ||
frame.add(groesse_l); | ||
|
||
groesse_f = new TextField("0", 15); | ||
groesse_f.setEditable(true); | ||
groesse_f.addFocusListener(this); | ||
frame.add(groesse_f); | ||
|
||
ergebnis_l = new Label("Ergebnis der Rechnung (PPI):"); | ||
frame.add(ergebnis_l); | ||
|
||
ergebnis_f = new TextField("Kein Ergebnis", 15); | ||
ergebnis_f.setEditable(false); | ||
frame.add(ergebnis_f); | ||
|
||
berechnen = new Button("Berechnen"); | ||
frame.add(berechnen); | ||
|
||
berechnen.addActionListener(this); | ||
|
||
frame.addWindowListener(this); | ||
|
||
frame.setTitle("PPI Rechner"); | ||
frame.setSize(350, 200); | ||
Dimension size = new Dimension(350, 200); | ||
frame.setMinimumSize(size); | ||
frame.setMaximumSize(size); | ||
frame.setPreferredSize(size); | ||
frame.setResizable(false); | ||
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("ppi.jpg"))); | ||
frame.setVisible(true); | ||
|
||
} | ||
|
||
public static void main(String[] args) { | ||
new PPICalculator(); | ||
} | ||
|
||
public void infoBox(String infoMessage) { | ||
ergebnis_f.setText("Kein Ergebnis"); | ||
JOptionPane.showMessageDialog(frame, infoMessage, "Fehler", | ||
JOptionPane.ERROR_MESSAGE); | ||
} | ||
|
||
public void focusGained(FocusEvent fe) { | ||
// Get what textfield got focus | ||
TextField t = (TextField) fe.getSource(); | ||
t.setBackground(Color.LIGHT_GRAY); | ||
t.select(0, t.getText().length()); | ||
} | ||
|
||
public void focusLost(FocusEvent fe) { | ||
// Get what textfield lost focus | ||
TextField t = (TextField) fe.getSource(); | ||
t.setBackground(Color.WHITE); | ||
} | ||
|
||
public void calc() throws Exception { | ||
PPI display = new PPI(breite_f.getText(), hoehe_f.getText(), groesse_f.getText()); | ||
double ergebnis = display.berechnen(); | ||
ergebnis_f.setText(Math.round(ergebnis) + " (" + ergebnis + ")"); | ||
|
||
} | ||
|
||
@Override | ||
public void actionPerformed(ActionEvent evt) { | ||
try { | ||
calc(); | ||
} catch (IllegalArgumentException e) { | ||
infoBox(e.getMessage()); | ||
} catch (ArithmeticException e) { | ||
infoBox(e.getMessage()); | ||
} catch (Exception e) { | ||
infoBox(e.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public void windowClosing(WindowEvent e) { | ||
e.getWindow().dispose(); | ||
System.exit(0); | ||
} | ||
|
||
@Override | ||
public void windowActivated(WindowEvent arg0) { } | ||
|
||
@Override | ||
public void windowClosed(WindowEvent arg0) { } | ||
|
||
@Override | ||
public void windowDeactivated(WindowEvent arg0) { } | ||
|
||
@Override | ||
public void windowDeiconified(WindowEvent arg0) { } | ||
|
||
@Override | ||
public void windowIconified(WindowEvent arg0) { } | ||
|
||
@Override | ||
public void windowOpened(WindowEvent arg0) { } | ||
|
||
} |