till tentamen 1 (5) Kurs Objektorienterad programmering Program DAI2 Läsår 2018/2019, lp 1 Examinator Uno Holmer Uppgift 1 (10 p) Ingen lösning ges. Se kurslitteraturen. Uppgift 2 (4+2+3+5 p) a) public boolean equals(object o) if ( this == o ) return true; else if ( o!= null && getclass() == o.getclass() ) Point other = (Point)o; return x == other.x && y == other.y; else return false; public int hashcode() return (123 + x)*37 + y; c) public Point clone() try Point p = (Point)super.clone(); p.modcount = 0; return p; catch ( CloneNotSupportedException e ) throw new InternalError(); d) public Polygon clone() try Polygon copy = (Polygon)super.clone(); copy.points = (ArrayList<Point>)points.clone(); for (int i = 0; i < points.size(); i++) copy.points.set(i,points.get(i).clone()); return copy; catch ( CloneNotSupportedException e ) throw new InternalError();
2 (5) Uppgift 3 (3+3+3+3 p) a) En av public class Student extends Person private String program; private int year; private int credits; public Student(String CID,String name,string address,string phone, String email,string program,int year,int credits) this.program = program; this.year = year; this.credits = credits; public class Employee extends Person private int salary; private int vacationdays; public Employee(String CID,String name,string address,string phone, String email,int salary,int vacationdays) this.salary = salary; this.vacationdays = vacationdays; public class Athlete extends Person private int height; private int weight; public Athlete(String CID,String name,string address,string phone, String email,int height,int weight) this.height = height; this.weight = weight; public static void main(string[] arg) Student s = new Student("123456-7890","Bob","Postgatan 20", "0123-4567890","bob@mail.org","D",2016,85); Employee e = new Employee("123456-7890","Bob","Postgatan 20", "0123-4567890","bob@mail.org",45000,35); Athlete a = new Athlete("123456-7890","Bob","Postgatan 20", "0123-4567890","bob@mail.org",168,74); s.setaddress("telefongränd 7"); e.setaddress("telefongränd 7"); a.setaddress("telefongränd 7");
3 (5) c) En av public class Student private String program; private int year; private int credits; public Student(Person personaldata,string program,int year,int credits) this.program = program; this.year = year; this.credits = credits; public class Employee private int salary; private int vacationdays; public Employee(Person personaldata,int salary,int vacationdays) this.salary = salary; this.vacationdays = vacationdays; public class Athlete private int height; private int weight; public Athlete(Person personaldata,int height,int weight) this.height = height; this.weight = weight; d) public static void main(string[] arg) Person bob = new Person("123456-7890","Bob","Postgatan 20", "0123-4567890","bob@mail.org"); Student s = new Student(bob,"D",2016,85); Employee e = new Employee(bob,45000,35); Athlete a = new Athlete(bob,168,74); bob.setaddress("telefongränd 7");
4 (5) Uppgift 4 (5+5 p) a) Anropet av f(2,3) returnerar 120. Anropskedja: Base.f(2,3) -> Sub.g(2+3) -> 2* Base.h(5) -> 4* Sub.f(5-1) -> 4 + 11 <- 15 <- 60 <- 120 <- 120 Motivering: Metoden f är överlagrad i Base. Bara den första matchar anropet med två heltalsargument. Denna metod är inte omdefinierad i Sub. Däremot är g omdefinierad i Sub och eftersom anropet görs för ett Sub-objekt väljs Sub.g. I Sub.g ärvs h från Base. Anropet av f i h går till Sub.f eftersom det fortfarande är samma Sub-objekt som tidigare som anropet görs för. Undantagen som kastas av en omdefinierad metod i en subklass måste vara kompatibla med basklassmetodens undantag. Subklassmetoden får kasta färre undantag och de som kastas måste vara av samma typer som basklassmetodens, eller subtyper till dem. T.ex. kan A.f kasta undantagen E2 och E3. Det är då tillåtet för B.f att deklarera att bara E2 kastas eftersom det ingår i A.f:s lista. För C.f blir det problematiskt eftersom E1 är basklass till E2 och därför är inte E1 kompatibelt med E2. Detta ger ett kompileringsfel. De två första anropen av func i main ger utskrifterna func catch E2: A.f throwed E2 func catch E3: B.f throwed E3
Uppgift 5 (14 p) 5 (5) public class Fire private Map<Integer,GroupAccount> database; private int lastgroup = 1; private int nooflabs; private String grader = "NN"; // Fixed for now. public Fire(int nooflabs) this.nooflabs = nooflabs; database = new TreeMap<>(); public int signup(string email,string password) for ( GroupAccount account : database.values() ) if ( account.getemail().equals(email) ) return -1; database.put(lastgroup, new GroupAccount(lastGroup,email,password,noOfLabs)); return lastgroup++; public String submit(int groupid,int labno,string content) throws UnknownAccountException, DuplicateSubmissionException, IllegalLaborationNoException if (! database.containskey(groupid) ) throw new UnknownAccountException( "Group " + groupid + " does not exist"); if ( labno < 1 labno > nooflabs ) throw new IllegalLaborationNoException("Illegal lab no:"+ labno); GroupAccount account = database.get(groupid); account.submit(new Submission(labNo,content,grader)); return grader; public void printresulttable() for ( Map.Entry<Integer,GroupAccount> e : database.entryset() ) System.out.print(e.getKey() + " "); for ( Submission s : e.getvalue() ) System.out.print(s == null? " - " : s.getstatus() + " "); System.out.println();