Uppgift 1 Lösningsförslag import java.awt.rectangle; public class ImprovedRectangle extends Rectangle { public ImprovedRectangle(int width, int height) { super(width, height); public ImprovedRectangle(int x, int y, int width, int height) { super(x, y, width, height); public int getarea() { return (int) (getwidth() * getheight()); public int getcircumference() { return (int) (2*(getWidth() + getheight())); //ImprovedRectangle 1 Lösningsförslag
Uppgift 2 Ja, vi borde definierat ett gränssnitt public interface Countable { public void count(); public void reset(); public int getvalue(); och låtit klasserna Counter, BoundedCounter och ChainedCounter implementerat detta gränssnitt enligt: public class Counter implements Countable { /*same code as before*/ public class BoundedCounter implements Countable { /*same code as before*/ public class ChainedCounter implements Countable { /*same code as before*/ Detta ger fördelen att datatyperna Counter, BoundedCounter och ChainedCounter alla är subtyper till datatypen Countable. Detta gör det möjligt att deklarera referensvariabler av datatypen Countable och tilldela dessa variabler referenser till objekt av datatypen Counter, BoundedCounter eller ChainedCounter. Används denna teknik blir alltså instanser av dessa tre klasser utbytbara. Man kan också tänka sig att låta BoundedCounter utöka Counter och ChainedCounter utöka BoundedCounter: public class Counter implements Countable { protected int value; /*same code as before*/ //Counter public class BoundedCounter extends Counter { protected int modulus; public BoundedCounter(int modulus) { super(); this.modulus = modulus; //constructor public void count() { value = (value + 1) % modulus; //count //BoundedCounter public class ChainedCounter extends BoundedCounter { private Counter next; public ChainedCounter(int init, int modulus, Counter next) { super(modulus); this.value = init; this.next = next; //constructor public void count() { value = (value + 1) % modulus; if (value == 0 && next!= null) next.count(); //count // ChainedCounter 2 Lösningsförslag
Uppgift 3 import java.awt.*; public class GeometricObject { private Color color; private Point position; protected GeometricObject(){ position = new Point(0,0); //placeras i punkten (0,0) color = Color.WHITE; //ges färgen vit protected GeometricObject(Point position, Color color){ this.position = position; this.color = color; public void setcolor(color color) { this.color = color; public Color getcolor() { return color; public void setposition(point position) { this.position = position; public Point getposition() { return position; public void move(point position) { this.position = position; return "Color: " + color + "\nposition: " + position; // GeometricObject 3 Lösningsförslag
import java.awt.*; public class Rectangle extends GeometricObject { private double width; private double length; public Rectangle() { this(1.0, 1.0); //ges bredden 1 och längden 1 public Rectangle(double width, double length){ this.width = width; this.length = length; public Rectangle(double width, double length, Point position, Color color){ super(position, color); this.width = width; this.length = length; public void setwidth(double width) { this.width = width; public double getwidth() { return width; public void setlength(double length) { this.length = length; public double getlength() { return length; public double findarea() { return width*length; public double findperimeter() { return 2*(width + length); return super.tostring() + "\nwidth: " + width + "\nlength: " + length; // Rectangle 4 Lösningsförslag
import java.awt.*; public class Circle extends GeometricObject { private double radius; public Circle(){ this.radius = 1.0; public Circle(double radius){ this.radius = radius; public Circle(double radius, Point position, Color color){ super(position, color); this.radius = radius; public void setradius(double radius) { this.radius = radius; public double getradius() { return radius; public double findarea() { return Math.PI*radius*radius; public double findperimeter() { return 2*radius*Math.PI; return super.tostring() + "\nradius: " + radius; // Circle import java.awt.*; public class Cylinder extends Circle { private double height; public Cylinder(){ this.height = 1.0; public Cylinder(double radius, double height){ super(radius); this.height = height; public Cylinder(double radius, double height, Point position, Color color){ super(radius, position, color); this.height = height; public void setheight(double height) { this. height = height; public double getheight () { return height; public double findarea() { return 2* super.findarea() + height*findperimeter(); public double findvolym() { return super.findarea() * height; return super.tostring() + "\nheight: " + height; // Cylinder 5 Lösningsförslag
Uppgift 4 a) b) //before: people!= null public static ArrayList<String> bornthisyear(arraylist<person> people, int year) { ArrayList<String> res = new ArrayList<String>(); for (Person p : people) { if (p.getyear() == year) res.add(p.getname()); return res; //bornthisyear //before: people!= null public static void removenames(arraylist<person> people, ArrayList<String> names) { int pos = 0; while (pos < people.size()) { if (isinlist(names, people.get(pos).getname())) people.remove(pos); else pos++; //removenames //before: names!= null private static boolean isinlist(arraylist<string> names, String name) { for (String elem: names) { if (elem.equals(name)) return true; return false; //isinlist Diskutera/förklara varför inte följande lösningsförslag fungerar? //before: people!= null public static void removenames(arraylist<person> people, ArrayList<String> names) { for (int pos = 0; pos < people.size(); pos++) { if (isinlist(names, people.get(pos).getname())) { people.remove(pos); //removenames Uppgift 5 public enum SuitColor { RED, BLACK; //SuitColor public enum Suit { HEARTS(SuitColor.RED), SPADES(SuitColor.BLACK), DIAMONDS(SuitColor.RED), CLUBS(SuitColor.BLACK); private SuitColor color; public Suit(SuitColor color) { this.color = color; public SuitColor getcolor() { return color; //Suit 6 Lösningsförslag
Uppgift 6 public interface Superpower { public void fly(); public void heal(); public void rescue(); //Superpover public class Human { public void eat(int calories) { public void drink(int volyme) { public void sleep(int minutes) { //Human public Superhero extends Human interface Superpower { public void fly() { public void heal() { public void rescue() { //Superhero 7 Lösningsförslag