Uppgift 1) a) [8, 11, 14] b) false true false c) The code swaps each pair of entries twice. Change the loop bound to arr.length / 2. d) The two backslashes need to be escaped, i.e. replaced with \\ Uppgift 2) a) public static boolean isPrime(int x) { if (x <= 1) return false; if (x % 2 == 0) return (x == 2); double squareRoot = Math.sqrt(x); for (int i = 3; i <= squareRoot; i++) { if (x % i == 0) return false; } return true; } b) public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a number"); int x = keyboard.nextInt(); while (x > 1) { // Find the largest prime factor of x for (int i = x; i > 1; i--) { if (isPrime(i) && x % i == 0) { System.out.print(i); System.out.print(" "); x = x / i; break; } } } } Uppgift 3) import java.util.*; import java.io.*; class Uppgift3{ public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyzåäö"; public static String encode(String text, int key){ String plainTxt = text.toLowerCase(); String cipher = ""; for(int i = 0; i < plainTxt.length(); i++){ int charPos = ALPHABET.indexOf(plainTxt.charAt(i)); int keyVal = (key + charPos) % ALPHABET.length(); char codeChar = ALPHABET.charAt(keyVal); cipher += codeChar; } return cipher; } public static void main(String[] args) { if (args.length != 3) { System.out.println("Felaktigt antal argument."); System.exit(0); } Scanner in = null; try{ // Fil för indata. in = new Scanner(new File(args[0])); } catch(FileNotFoundException e){ System.out.println(e.getMessage()); System.exit(0); } PrintStream w = null; try{ // Fil för utdata w = new PrintStream(new File(args[1])); } catch(FileNotFoundException e){ System.out.println(e.getMessage()); System.exit(0); } // Nyckel för kryptering. int key = Integer.parseInt(args[2]); while(in.hasNext()){ String s = encode(in.next(),key); w.print(s + " "); } in.close(); w.close(); } } Uppgift 4) import java.util.*; public abstract class Gate { protected String name; public Gate(String name) { this.name = name; } public abstract boolean getOutput(); public String getName() { return name; } } class ConstantGate extends Gate { private boolean value; public ConstantGate(String name) { super(name); } public void set(boolean v) { value = v; } public boolean getOutput() { return value; } } abstract class BasicGate extends Gate { protected List input = new ArrayList(); protected BasicGate(String name) { super(name); } protected void checkInput() { if (input.size() < 2) throw new IllegalStateException("Gate " + name + ": Input signal(s) missing"); } protected abstract boolean calculateValue(); public void setInput(Gate g) { input.add(g); } public boolean getOutput() { checkInput(); return calculateValue(); } } class AndGate extends BasicGate { public AndGate(String name) { super(name); } protected boolean calculateValue() { for (Gate g : input) if (!g.getOutput()) return false; return true; } } class OrGate extends BasicGate { public OrGate(String name) { super(name); } protected boolean calculateValue() { for (Gate g : input) if (g.getOutput()) return true; return false; } } class NotGate extends BasicGate { protected NotGate(String name) { super(name); } @Override protected void checkInput() { if (input.size() != 1) throw new IllegalStateException("Gate " + name + ": Illegal number of input signals"); } protected boolean calculateValue() { return !input.get(0).getOutput(); } } Uppgift 5) public class Lines extends JPanel { private int n; public Lines(int n) { this.n = n; } public void paintComponent(Graphics g) { super.paintComponent(g); Insets i = getInsets(); int w = getWidth() - i.left-i.right; int h = getHeight() - i.top - i.bottom; int dx = w/(n+1); int dy = h/(n+1); for (int j = 1; j <= n; j++) g.drawLine(i.left+j*dx, i.top+h, i.left+w, i.top+h-j*dy); } } Uppgift 6) import java.util.*; public class Airport { private String name; private NavigableSet departures = new TreeSet(); private Map> flightsTo = new HashMap>(); public Airport(String n) { name = n; } public String getName() { return name; } public NavigableSet getDepartures() { return departures; } public NavigableSet getDepartures(String to) { return flightsTo.get(to); } public void addFlight(Flight f) { departures.add(f); if (!flightsTo.containsKey(f.getDestination())) flightsTo.put(f.getDestination(), new TreeSet()); flightsTo.get(f.getDestination()).add(f); } public void removeFlight(Flight f) { departures.remove(f); NavigableSet e = flightsTo.get(f.getDestination()); if (e != null) { e.remove(f); if (e.isEmpty()) flightsTo.remove(f.getDestination()); } } }