Lektion 6 - Programmeringsteknik MN1, ht 2003
Daniel Deogun
danield@it.uu.se
30 oktober 2003
Inneh˚ all
1 Struktur f¨or inlupp 3 3
2 Hur ritar man en cirkel grafiskt i Java? 3
2.1 OvalTest.java . . . 3 2.2 Oval.java . . . 3 2.3 Output . . . 4
3 Stack - en h¨og av element 4
3.1 Driver.java . . . 4 3.2 Stack.java . . . 4 3.3 Output . . . 6 4 Polsk Notation - ett annorlunda skrivs¨att 6 4.1 Compute.java . . . 6 4.2 Output . . . 8
1 Struktur f¨ or inlupp 3
Se lektion 4. Modifiera strukturen s˚a att en cirkul¨ar bilbana anv¨ands.
2 Hur ritar man en cirkel grafiskt i Java?
2.1 OvalTest.java
import extra.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class OvalTest extends JFrame { Oval bana = new Oval();
JLabel l = new JLabel("Min bana", JLabel.CENTER);
public OvalTest() {
Container c = getContentPane();
l.setOpaque(true);
l.setBackground(Color.white);
c.add(bana,"Center");
c.add(l,"South");
pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String [] args) { OvalTest b = new OvalTest();
} }
2.2 Oval.java
import extra.*;
import javax.swing.*;
import java.awt.*;
public class Oval extends JPanel { public Oval () {
setPreferredSize(new Dimension(300,300));
}
public void paintComponent(Graphics g) { super.paintComponent(g);
int radius =
Math.min(getSize().width/2, getSize().height/2);
g.setColor(Color.gray);
g.fillOval(10, 10, radius*2-20, radius*2-20);
g.setColor(getBackground());
g.fillOval(20, 20, radius*2-40, radius*2-40);
} }
2.3 Output
Ladda ner koden och se sj¨alva.
3 Stack - en h¨ og av element
3.1 Driver.java
import extra.Std;
public class Driver {
public static void main(String[] args) { Stack s = new Stack(10);
Std.out.println("Is stack full? " + s.isFull());
Std.out.println("Is stack empty? " + s.isEmpty());
s.push(10);
s.push(20);
s.push(-42);
Std.out.println("Pop the stack: " + s.pop());
Std.out.println("Pop the stack: " + s.pop());
Std.out.println("Pop the stack: " + s.pop());
} //end main } //end class
3.2 Stack.java
/**
* This class implements a stack.
*
* @author Daniel Deogun (danield@it.uu.se)
* @version 1.0
*/
public class Stack { /**
* Instance variable - the stack
*/
private String[] stack;
/**
* Instance variable - the size of the stack
*/
private int size;
/**
* Instance variable - points to the top of the stack
*/
private int stackPointer = 0;
/**
* Constructor - used for building a Stack object.
*
* @param size The size of the stack
*/
public Stack(int size) { this.size = size;
stack = new String[size];
} //end constructor /**
* This method implements the push operation.
*
* @param element The element to push onto the stack
*/
public void push(String element) { if(stackPointer < stack.length)
stack[++stackPointer] = element;
} //end push /**
* This method implements the push operation.
*
* @param element The element to push onto the stack
*/
public void push(int element) { if(stackPointer < stack.length)
stack[++stackPointer] = "" + element;
} //end push /**
* This method implements the pop operation.
*
* @return The top element, <code>null</code> if stack is empty
*/
public String pop() { if(!isEmpty())
return stack[stackPointer--];
return null;
} //end pop /**
* This method checks if the stack is empty
*
* @return <code>true</code> if the stack is empty, <code>false</code>
* otherwise.
*/
public boolean isEmpty() { return stackPointer < 0;
} //end isEmpty /**
* This method checks if the stack is full
*
* @return <code>true</code> if the stack is full, <code>false</code>
* otherwise.
*/
public boolean isFull() {
return stackPointer >= size;
} //end isFull } //end class
3.3 Output
Is stack full? false Is stack empty? false Pop the stack: -42 Pop the stack: 20 Pop the stack: 10
4 Polsk Notation - ett annorlunda skrivs¨ att
4.1 Compute.java
import java.util.StringTokenizer;
/**
* This class provide routines for evaluating an
* expression in Polish notation.
*
* @author Daniel Deogun (danield@it.uu.se)
* @version 1.0
*/
public class Compute { /**
* Constant - the plus operator
*/
private final String PLUS = "+";
/**
* Constant - the minus operator
*/
private final String MINUS = "-";
/**
* Constant - the multiplication operator
*/
private final String MULT = "*";
/**
* Constant - the division operator
*/
private final String DIV = "/";
/**
* Instance variable - the stack
*/
private Stack stack;
/**
* This method evaluates an expression written in
* in Polish notation.
*
* @param expression The expression in Polish notation
* @return The result of the evaluation as an integer.
*/
public int polishNotation(String expression) {
StringTokenizer st = new StringTokenizer(expression);
stack = new Stack(expression.length());
int number = 0;
String token;
do {
if(st.hasMoreTokens()) { token = st.nextToken();
stack.push(token);
if(!operator(token)) { number++;
if(number == 2) { evaluate();
number--;
} } else
number = 0;
} else
token = evaluate();
} while(!stack.isEmpty());
return Integer.parseInt(token);
} //end polishNotation
/**
* This method checks if <code>exp</code> is an operator.
*
* @param exp A string that may be an operator.
* @return <code>true</code> if the parameter is an operator,
* <code>false</code> otherwise.
*/
private boolean operator(String exp) {
return exp.equals(PLUS) || exp.equals(MINUS) || exp.equals(MULT) ||
exp.equals(DIV);
} //end operator /**
* This method pops the three top most elements off the stack
* and computes the result. The result is then pushed on the stack.
*
* @return The result of the evaluation as a String.
*/
private String evaluate() { String t2 = stack.pop();
String t1 = stack.pop();
String op = stack.pop();
String res="";
if(t1 == null) return t2;
if(op.equals(PLUS))
res += Integer.parseInt(t1) + Integer.parseInt(t2);
else if(op.equals(MINUS))
res += Integer.parseInt(t1) - Integer.parseInt(t2);
else if(op.equals(MULT))
res += Integer.parseInt(t1) * Integer.parseInt(t2);
else
res += Integer.parseInt(t1) / Integer.parseInt(t2);
stack.push(res);
return res;
} //end evaluate } //end class
4.2 Output
Please enter an expression in Polish notation - + 3 4 / 100 25
Answer: 3 ----
Please enter an expression in Polish notation + * 2 3 - / 4 3 2
Answer: 5