Uppgift 1 Lösningsförslag till tentamen 121217 a) Utskriften blir: a = 5 och b = 10 a = 5 och b = 10 b) Utskriften blir 1 2 3 4 5 5 2 3 4 1 c) Utskriften blir 321 Uppgift 2 Med användning av dialogrutor: import java.util.*; public class QuadraticEquation { while(true) { String indata = JOptionPane.showInputDialog("Ange koeffienterna a, b och c: "); if (indata == null) break; Scanner sc = new Scanner(indata); double a = sc.nextdouble(); double b = sc.nextdouble(); double c = sc.nextdouble(); if (a == 0) JOptionPane.showMessageDialog(null, "Detta är ingen andragradsekvation!\n Försök igen!"); computeroots(a, b, c); //while //main public static void computeroots(double a, double b, double c) { double D = Math.pow(b, 2) / Math.pow(2*a, 2) - c / a; if (D == 0) { JOptionPane.showMessageDialog(null, "Har en dubbelrot i " + -b/(2 * a)); if (D > 0) { JOptionPane.showMessageDialog(null, "Rötterna är " + (-b / (2*a) + Math.sqrt(D)) + " och " + (-b / (2*a) - Math.sqrt(D)) ); { double real = -b / (2*a); double imaginary = Math.sqrt(Math.abs(D)); JOptionPane.showMessageDialog(null, "Imaginära rötter i " + real + " + " + imaginary + "i \n" + "och " + real + " - " + imaginary + "i."); // computeroots // QuadraticEquation
Med användning av System.in och System.out: import java.util.*; public class QuadraticEquation { while(true) { System.out.print("Ange koeffienterna a, b och c: "); Scanner sc = new Scanner(System.in); if (!sc.hasnext()) break; double a = sc.nextdouble(); double b = sc.nextdouble(); double c = sc.nextdouble(); if (a == 0) System.out.println("Detta är ingen andragradsekvation!\n Försök igen!"); computeroots(a, b, c); //while //main public static void computeroots(double a, double b, double c) { double D = Math.pow(b, 2) / Math.pow(2*a, 2) - c / a; if (D == 0) { System.out.println("Har en dubbelrot i " + -b/(2 * a)); if (D > 0) { System.out.println("Rötterna är " + (-b / (2*a) + Math.sqrt(D)) + " och " + (-b / (2*a) - Math.sqrt(D)) ); { double real = -b / (2*a); double imaginary = Math.sqrt(Math.abs(D)); System.out.println("Imaginära rötter i " + real + " + " + imaginary + "i \n" + "och " + real + " - " + imaginary + "i."); // computeroots // QuadraticEquation
Uppgift 3 public class FuelTank { private int capacity; private int fuellevel; public FuelTank(int capacity) { this.capacity = capacity; fuellevel = 0; //konstruktor public int getcapacity() { return capacity; //getcapacity public int getfuellevel() { return fuellevel; //getfuel public int fillfuel() { int fillingvolume = capacity - fuellevel; fuellevel = capacity; return fillingvolume; //fillfuel public void emptyfuel(int volume) { if ( fuellevel - volume >= 0) throw new IllegalArgumentException(); fuellevel = 0; //emptyfuel public int fillingrate() { return 100 *fuellevel / capacity; //fillingrate public String tostring() { return "Kapacitet: " + capacity + " liter\n" + "Återstående volym: " + fuellevel + " liter\n" + "Fyllnadsgrad: " + fillingrate() +"%"; //tostring //FuelTank Kommentar: Datatypen på instansvariablerna kan väljas till double. Detta medför då att inparameternas typer och returtyperna ändras till double för berörda metoder. Vidare förändras tostring metoden, eftersom värdena på instansvariablerna skrivs ut som heltal. Uppgift 4 import java.io.*; import java.util.scanner; public class Faulty { public static void main(string[] args) throws FileNotFoundException { String file = args[0]; Scanner in = new Scanner(new File(file)); int totally = 0; int faultybefore = 0; int faultyafter = 0; while (in.hasnextdouble() ) { double diff = in.nextdouble(); if (Math.abs(diff) >= 0.001) faultybefore++; if (Math.abs(diff) >= 0.0001) faultyafter++; in.close(); System.out.println("Ej accepterade axlar ökar från " + 100* (double) faultybefore/ totally + " % till " + 100 * (double) faultyafter/ totally + " %."); //main //Faulty
Uppgift 5 a) public int[] limitamplitud(int[] samples, int limit) { int[] newsamples = new int[samples.length]; for (int i = 0; i < samples.length; i = i + 1) { if (samples[i] > limit) newsamples[i] = limit; if (samples[i] < -limit) newsamples[i] = -limit; newsamples[i] = samples[i] return newsamples; // limitamplitud b) public static int[][][] rotate90(int[][][] picture) { int[][][] newpicture = new int[picture[0].length][picture.length][3]; for (int row = 0; row < picture.length; row++) { for (int col = 0; col < picture[row].length; col++) { for(int colors = 0; colors < picture[row][col].length; colors = colors + 1) newpicture[col][row][colors] = picture[row][picture[row].length - 1 - col][colors]; return newpicture; //rotate90
Uppgift 6 import java.awt.*; import java.awt.event.*; public class Team extends JPanel implements ActionListener { private JButton namebutton; private JLabel resultlabel; private int nrofgoals = 0; public Team (String teanname) { namebutton = new JButton(teanName); resultlabel = new JLabel("0", JLabel.CENTER); setlayout(new GridLayout(2, 1)); add(namebutton); add(resultlabel); namebutton.setbackground(color.yellow); resultlabel.setbackground(color.pink); namebutton.setbackground(color.pink); resultlabel.setbackground(color.yellow); resultlabel.setopaque(true); namebutton.addactionlistener(this); Font font = new Font("Times", Font.PLAIN, 26); namebutton.setfont(font); font = new Font("Times", Font.PLAIN, 36); resultlabel.setfont(font); //constructor public void actionperformed(actionevent e) { if (e.getsource() == namebutton) { nrofgoals = nrofgoals + 1; resultlabel.settext(" " + nrofgoals); //actionperformed //Team import java.awt.*; public class Match extends JFrame { public Match(String nameofhometeam, String nameofopponetteam) { getcontentpane().setlayout(new GridLayout(1, 2, 5, 5)); Team hometeam = new Team(nameOfHomeTeam); Team opponetteam = new Team(nameOfOpponetTeam); getcontentpane().add(hometeam); getcontentpane().add(opponetteam); pack(); setdefaultcloseoperation(exit_on_close); setvisible(true); //konstruktor Match v = new Match("Malmö FF", "IFK Göteborg"); //main //Match