REVIEW QUESTIONS FOR WEEKS 1-5

Following are some questions you are expected to be able to solve towards assessment of the course.

Questions: Java basics

Consider the following definitions:

    int x=3;
    iny y=x;
    y+=1;
    int[] arr1={1,2,3};
    int[] arr2=arr1;
    arr2[0]=5;

What would be the output, if the following statements are executed after the definitions above:

    System.out.println(x);
    System.out.println(y);
    System.out.println(arr1.length);
    System.out.println(arr1[0]);
    System.out.println(arr2[0]);

Questions: classes and instances

Consider the following definitions:

    class C {
        public static int a=0;
        public int x;
        public C(int x) { 
            a += 1;
            this.x = x;
        }
    }

Question 1: How many objects will be in the memory after the following commands are executed:

    C c1=new C(1);
    C c2;
    c1=new C(2);
    C c3=new C(3);
    C c4=c1;

Question 2: What will be the output when each of the following statements are executed after all of the above:

    c1.x+=1;
    System.out.println(c1.a);
    System.out.println(c3.x);
    System.out.println(c1.x);
    System.out.println(c3.a);
    System.out.println(c4==c1);
    System.out.println(c3!=c2);

Questions: reading code

Find the mistake in each of the questions, or indicate that they are true (i.e. will compile without errors):

RC-1

    double x="abc";

RC-2

    interface I { }
    class C {
        I i=new I();
        int j=1;
    }

RC-3

    interface I { }
    class C implements I{ }
    class D {
        C x= new C();
        I y=new I();
    }

RC-4

    interface I { }
    class C implements I{ }
    class D extends C { }
    class E {
        C x= new D();
    }

RC-5

    interface I { }
    class C implements I{ }
    class D {
        C x=new I();
    }

RC-6

    interface I { 
        public int f();
    }
    class C implements I{ }
    class D {
        I x=new C();
    }

RC-7

    class C {
        C(int x) {
            this.x=x;
        }
    }

Questions interfaces and polymorphism

Consider the following definitions:

    interface I1{
        public void f1();
    }
    interface I2 extends I1 {
        public void f2();
    }
    class A { 
    }
    class B extends A { 
        public void f1() { }
    }

Find the error in each of the following declarations: Declaration 1

    class C extends A, B { }

Declaration 2

    class D extends A implements I1 { 
        public int f1() {return 1;}
    }

Declaration 3

    class E extends B implements I2 { }

Declaration 4

    class F extends B implements I2 { 
        private void f2() { }
    }

Questions: type compatibility

Consider the definitions in the above questions. For each of the below types, determine type compatibility, that is determine the list of types for which the instance of given type is compatible with, ie. its instance can be used as a value for a variable of those types.

CASE STUDIES

Following case studies demonstrate program design practices starting with a problem statement. You will be expected to perform -parts or whole- of this process during assessments (e.g. exams).

Case study: A rectangle

A rectangle is defined with two dimensions: width and height. Write a class to represent it, with a proper constructor.

Solution

    class Rectangle {
        double width, height;
        Rectangle(double width, double height) {
            this.width=width;
            this.height=height;
        }
    }

Case study: A rectangle and an interface

Improve your rectangle class to implement the following interface:

    interface Shape {
        public double area();
        public double circumference();
    }

Solution

    class Rectangle implements Shape{
        double width, height;
        Rectangle(double width, double height) {
            this.width=width;
            this.height=height;
        }
        public double area() { return width*height;}
        public double circumference() { return 2*width+2*height;}
    }

Case study: Family relations

  1. Write a class to represent a Person, who is identified with a name, but your class must also represent the person's mother and father (if known). Implement useful constructors and/or methods for your class to manipulate family relations.

  2. Now write the commands to represent the person object in the following family:

    Ali: parents unknown
    Ayse: parents unknown
    Zeynep: parents are Ali and Ayse
    Berk: parents are Ali and Ayse

Solution

    class Person {
        String name;
        Person father, mother;
        Person(String name, Person father, Person mother) {
            this.name=name;
            this.father=father;
            this.mother=mother;
        }
        Person (String name) {
            this(name, null, null);
        }
    }

    Person ali=new Person("ali");
    Person ayse=new Person("ayse");
    Person zeynep= new Person("zeynep",ali,ayse);
    Person berk= new Person("berk",ali,ayse);

Case study: A product

You are expected to write a class to represent a product in a supermarket. A supermarket contains products, each of which is represented with a name, a barcode (unique string), and each has a price, and a unit for the price (e.g. kg for vegetables, piece for chocolates).

Solution

    class Product {
        String name, barcode, unit;
        double price;
        Product(String name, String barcode, double price, String unit) {
            this.name=name;
            this.barcode=barcode;
            this.price=price;
            this.unit=unit;
        }
    }

Case study: Roads and cities

Consider the roads connecting cities. A road connects two cities, and has a certain distance. A city on the other hand has a name.

Question A: Write these two classes.

Question B: Write the necessary commands to create the following roads, using the classes you have written above:

    İstanbul - İzmir: 540 km
    İstanbul - Ankara: 350 km
    İzmir - Ankara: 450 km

Solution

    class City {
        String name;
        City(String name) { this.name= name;}
    }
    class Road {
        City c1,c2;
        double distance;
        Road(City c1, City c2, double distance) { 
            this.cq=c1; 
            this.c2=c2; 
            this.distance=distance;
        }
    }

    City istanbul=new City("İstanbul");
    City izmir=new City("İzmir");
    City ankara=new City("Ankara");
    Road r1=new Road(istanbul,izmir,540);
    Road r2=new Road(istanbul,ankara,350);
    Road r1=new Road(ankara,izmir,450);