Skip to content

Instantly share code, notes, and snippets.

@MartinIngesen
Last active August 29, 2015 14:02
Show Gist options
  • Save MartinIngesen/3ea257407e20dbaff68d to your computer and use it in GitHub Desktop.
Save MartinIngesen/3ea257407e20dbaff68d to your computer and use it in GitHub Desktop.

Eksamen PG2100 2013

Oppgave 1

a)

  1. B
  2. A og C
  3. C

b)

Det vil bli skrevet Child begge gangene.

c)

  1. For å opprette en ny subklasse av Motorisert må subklassen innehold en metode for fyllDrivstoff()

  2.  

     public class BilKlient{
     	public static void main(String[] args){
     		Bil bil = new Bil();
     		bil.start();
     		bil.fyllDrivstoff();
     		bil.stopp();
     	}
     }
    
  3.  

     public class FarkostKlient{
     	public static void main(String[] args){
     		
     		Fly fly = new Fly()
     		Baat baat = new Baat();
     		Bil bil = new Bil();
     		Seilbaat seilbaat = new Seilbaat();
     		Sykkel sykkel = new Sykkel();
     		
     		ArrayList<Farkost> farkost = new ArrayList<Farkost>();
     		
     		farkost.add(fly);
     		farkost.add(baat);
     		farkost.add(bil);
     		farkost.add(seilbaat);
     		farkost.add(sykkel);
     		
     		foreach(Farkost fk : farkost){
     			fk.start();
     			fk.stopp();
     		}
     		
     	}
     }
    

Oppgave 2

a)

	public class Fan{
		private int regNr;
		private int speed;
		private boolean on;
		private double radius;
		private String color;
		
		public Fan(){
			this(0, 1, false, 0.5, "blue");
		}
		public Fan(int regNr, int speed, boolean on, float radius, String color){
			setRegNr(regNr);
			setSpeed(speed);
			setOn(on);
			setRadius(radius);
			setColor(color);
		}
		public void setRegNr(int regNr){
			if(regNr >= 0){
				this.regNr = regNr;
			}else{
				System.out.println("RegNr not a valid number. Must be >= 0");
			}
			
		}
		public void setSpeed(int speed){
			if(speed == 1 || speed == 2 || speed == 3){
				this.speed = speed;
			}else{
				System.out.println("Speed is not a valid speed. (1, 2 or 3)");
			}
		}
		public void setOn(boolean on){ this.on = on; }
		public void setRadius(double radius){ this.radius = radius; }
		public void setColor(String color){ this.color = color; }
		
		public int getRegNr(){ return regNr; }
		public int getSpeed(){ return speed; }
		public boolean getOn(){ return on; }
		public double getRadius(){ return radius; }
		public String getColor(){ return color; }
		
		public String toString(){
			return "regNr: " + getRegNr() + " speed: " + getSpeed() + " on: " + getOn() + " radius: " + getRadius() + " color: " + getColor();
		}
		
		public boolean equals(Object object){
			if(!(object instanceof this)) return false;
			if(object == this) return true;
		
			return getRegNr() == ((Fan) object).getRegNr();
		}
		
	}

b)

public class Klient{
	public static void main(String[] args){
		Fan fan1 = new Fan();
		Fan fan2 = new Fan(1, 3, true, 1.1, "green");
		
		fan1.setSpeed(2);
		fan1.setSpeed(5); // not valid, speed is still 2.
		
		fan1.setOn(true);
		fan1.setRadius(0.54);
		fan1.setColor("Light blue");
		
		fan2.setOn(false);
		
		system.out.println("Are fans the same?: " + fan1.equals(fan2)); // false
		
		fan1.setRegNr(1);
		fan1.setRegNr(4); // Doesnt work. RegNr still 1
		
		system.out.println("Are fans the same?: " + fan1.equals(fan2)); // true
		
		system.out.println("Get fan2 speed" + fan2.getSpeed());
		
		fan1.toString();			
	}
}

Oppgave 3

public class Kalkulator extends JFrame implements ActionListener{

	JTextField innskuddInput;
	JTextField aarInput;
	JTextField renteInput;
	JTextField saldoInput;

	public static void main(String[] args){
		new Kalkulator();
	}
	public Kalkulator(){
		super("Oppgave3");
		
		JPanel inputs = new JPanel(new GridLayout(2, 4));
		
		JLabel innskudd = new JLabel("Innskudd");
		JLabel aar = new JLabel("Antall år");
		JLabel rente = new JLabel("Årlig rente");
		JLabel saldo = new JLabel("Saldo i fremtiden");
		
		innskuddInput = new JTextField(10);
		aarInput = new JTextField(10);
		renteInput = new JTextField(10);
		saldoInput = new JTextField(10);
		
		inputs.add(innskudd);
		inputs.add(innskuddInput);
		inputs.add(aar);
		inputs.add(aarInput);
		inputs.add(rente);
		inputs.add(renteInput);
		inputs.add(saldo);
		saldoInput.setEditable(false);
		inputs.add(saldoInput);
		
		JPanel buttons = new JPanel(new GridLayout(1, 3));
		
		JButton beregn = new JButton("Beregn");
		JButton reset = new JButton("Reset");
		JButton avslutt = new JButton("Avslutt");
		
		beregn.addActionListener(this);
		reset.addActionListener(this);
		avslutt.addActionListener(this);
		
		buttons.add(beregn);
		buttons.add(reset);
		buttons.add(avslutt);
		
		add(inputs, BorderLayout.NORTH);
		add(buttons, BorderLayout.SOUTH);
		
		pack();
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent a){
		switch(a.getActionCommand()){
			case "Avslutt":
				System.exit(0);
				break;
			case "Reset":
				innskuddInput.setText("");
				aarInput.setText("");
				renteInput.setText("");
				break;
			case "Beregn":
				double innskudd = Double.parseDouble(innskuddInput.getText());
				double aar = Double.parseDouble(aarInput.getText());
				double rente = Double.parseDouble(renteInput.getText());
				saldoInput.setText(""+innskudd * Math.pow(1+(rente/100), aar));
				break;
		}
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment