Commits

Nick Raptis committed 784a9f8

Added more solutions to past exam

Comments (0)

Files changed (8)

src/paliathemata/java_2/a_2/Book.java

+package paliathemata.java_2.a_2;
+
+import javax.swing.JOptionPane;
+
+public class Book extends Publication {
+    private int pages;
+
+    public Book() {
+    }
+
+    public Book(String title, String author, double price, int pages) {
+        super(title, author, price);
+        this.pages = pages;
+    }
+
+    @Override
+    public void getData() {
+        super.getData();
+        String resp = JOptionPane.showInputDialog("Eisagete arithmo selidwn");
+        pages = Integer.parseInt(resp);
+    }
+
+    @Override
+    public void printData() {
+        super.printData();
+        System.out.println("Ar. selidwn\t: " + pages);
+    }
+
+    public int getPages() {
+        return pages;
+    }
+
+    public void setPages(int pages) {
+        this.pages = pages;
+    }
+
+}

src/paliathemata/java_2/a_2/Main.java

+package paliathemata.java_2.a_2;
+
+import javax.swing.JOptionPane;
+
+/*
+ * Edw einai o aplos tropos
+ * O periplokos einai stis omadas B
+ */
+public class Main {
+
+    public static void main(String[] args) {
+        //gia dokimi bale apla [1]
+        Publication [] pubs = new Publication[10];
+
+        for (int i=0; i<pubs.length; i++) {
+
+            String resp = JOptionPane.showInputDialog(
+                    "Antikeimeno "+(i+1)+"/"+pubs.length+
+                    "  Eisagete 'Book' gia biblio "+
+                    "h' 'Tape' gia kasseta");
+            char r = resp.toLowerCase().charAt(0);
+
+            //An den balei kati pou arxizei apo t, dialegei panta Biblio
+            //Sto thema tou B to kanw kalytera
+            if (r == 't') {
+                pubs[i] = new Tape();
+            } else {
+                pubs[i] = new Book();
+            }
+
+            pubs[i].getData();
+        }
+
+        for (int i=0; i<pubs.length; i++) {
+            System.out.println("Antikeimeno "+(i+1)+"/"+pubs.length);
+            pubs[i].printData();
+            System.out.println();
+        }
+    }
+
+}

src/paliathemata/java_2/a_2/Publication.java

+package paliathemata.java_2.a_2;
+
+import javax.swing.JOptionPane;
+
+public class Publication {
+    private String title;
+    private String author;
+    private double price;
+
+    public Publication() {
+    }
+
+    public Publication(String title, String author, double price) {
+        this.title = title;
+        this.author = author;
+        this.price = price;
+    }
+
+    public void getData() {
+        //Aploikos tropos, mporei na petaksei lathi se 500 simeia :P
+        //Sta themata B ftiaxnw ena poio periploko
+        title = JOptionPane.showInputDialog("Eisagete titlo");
+        author = JOptionPane.showInputDialog("Eisagete syggrafea");
+        String resp = JOptionPane.showInputDialog("Eisagete timi");
+        price = Double.parseDouble(resp);
+    }
+
+    public void printData() {
+        System.out.println("Titlos\t\t: " + title);
+        System.out.println("Syggrafeas\t: " + author);
+        System.out.println("Timi\t\t: " + price);
+    }
+
+    public String getAuthor() {
+        return author;
+    }
+
+    public void setAuthor(String author) {
+        this.author = author;
+    }
+
+    public double getPrice() {
+        return price;
+    }
+
+    public void setPrice(double price) {
+        this.price = price;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+}

src/paliathemata/java_2/a_2/Tape.java

+package paliathemata.java_2.a_2;
+
+import javax.swing.JOptionPane;
+
+public class Tape extends Publication {
+    private double minutes;
+
+    public Tape() {
+    }
+
+    public Tape(String title, String author, double price, double minutes) {
+        super(title, author, price);
+        this.minutes = minutes;
+    }
+
+    @Override
+    public void getData() {
+        super.getData();
+        String resp = JOptionPane.showInputDialog("Eisagete diarkeia se lepta");
+        minutes = Double.parseDouble(resp);
+    }
+
+    @Override
+    public void printData() {
+        super.printData();
+        System.out.println("Lepta\t\t: " + minutes);
+    }
+
+    public double getMinutes() {
+        return minutes;
+    }
+
+    public void setMinutes(double minutes) {
+        this.minutes = minutes;
+    }
+
+}

src/paliathemata/java_2/b_2/DVD.java

+package paliathemata.java_2.b_2;
+
+public class DVD extends Movie {
+    private int region;
+
+    public DVD() {
+    }
+
+    public DVD(String title, String director, double price, int region) {
+        super(title, director, price);
+        this.region = region;
+    }
+
+    @Override
+    public void getData() {
+        super.getData();
+        region = askForInt("Dwste perioxi xrisis");
+    }
+
+    @Override
+    public void printSpecific() {
+        System.out.println("Perioxi\t\t: " + region);
+    }
+
+    public int getRegion() {
+        return region;
+    }
+
+    public void setRegion(int region) {
+        this.region = region;
+    }
+
+}

src/paliathemata/java_2/b_2/EMovie.java

+package paliathemata.java_2.b_2;
+
+public class EMovie extends Movie {
+    private double sizeMb;
+
+    public EMovie() {
+    }
+
+    public EMovie(String title, String director, double price, double sizeMb) {
+        super(title, director, price);
+        this.sizeMb = sizeMb;
+    }
+
+    @Override
+    public void getData() {
+        super.getData();
+        sizeMb = askForDouble("Dwste megethos se MB");
+    }
+
+    @Override
+    public void printSpecific() {
+        System.out.println("Megethos (MB)\t: " + sizeMb);
+    }
+
+    public double getSizeMb() {
+        return sizeMb;
+    }
+
+    public void setSizeMb(double sizeMb) {
+        this.sizeMb = sizeMb;
+    }
+ 
+}

src/paliathemata/java_2/b_2/Main.java

+package paliathemata.java_2.b_2;
+
+import javax.swing.JOptionPane;
+
+/*
+ * Edw einai o periplokos tropos
+ * O aplos einai stis omadas A
+ */
+public class Main {
+
+    public static void main(String[] args) {
+        //gia dokimi bale apla [1]
+        Movie [] movies = new Movie[14];
+
+        String [] choices = new String [] {"DVD","EMovie"};
+        for (int i=0; i<movies.length; i++) {
+            int choice = JOptionPane.showOptionDialog(null,
+                    "Tainia "+(i+1)+"/"+movies.length+"  Dialekste typo",
+                    null, JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE,
+                    null, choices, 0);
+            if (choice == 0) {
+                movies[i] = new DVD();
+            } else {
+                movies[i] = new EMovie();
+            }
+            movies[i].getData();
+        }
+
+        for (int i=0; i<movies.length; i++) {
+            System.out.println("Tainia "+(i+1)+"/"+movies.length);
+            movies[i].printData();
+            System.out.println();
+        }
+    }
+
+}
+

src/paliathemata/java_2/b_2/Movie.java

+package paliathemata.java_2.b_2;
+
+import javax.swing.JOptionPane;
+
+public abstract class Movie {
+    private String title;
+    private String director;
+    private double price;
+
+    public Movie() {
+    }
+
+    public Movie(String title, String director, double price) {
+        this.title = title;
+        this.director = director;
+        this.price = price;
+    }
+
+    //Xrisimopoiw merikes methodous gia na ginei swsta i douleia
+    //Periploko, mia pio apli lysi einai sta themata tis A
+    public void getData() {
+        title = askForString("Dwste titlo");
+        director = askForString("Dwste skinotheti");
+        price = askForDouble("Dwste timi");
+    }
+
+    public void printData() {
+        System.out.println("Titlos\t\t: " + title);
+        System.out.println("Skinothetis\t: " + director);
+        printSpecific();
+        System.out.println("Timi\t\t: " + price);
+    }
+
+    /*
+     * Override to print subclass-specific information
+     */
+    public abstract void printSpecific();
+
+    public static String askForString (String prompt){
+        String resp;
+        do {
+            resp = JOptionPane.showInputDialog(prompt);
+        } while (resp == null); //Cancel has been pressed, reiterate
+        return resp;
+    }
+
+    public static int askForInt (String prompt){
+        String resp;
+        do {
+            resp = JOptionPane.showInputDialog(prompt);
+        } while (resp == null); //Cancel has been pressed, reiterate
+        return Integer.getInteger(resp, 0); //Returns 0 if input unparsabble
+    }
+
+    public static double askForDouble (String prompt){
+        String resp;
+        double r=0;
+        do {
+            resp = JOptionPane.showInputDialog(prompt);
+            try {
+                r = Double.parseDouble(resp);
+            } catch (NumberFormatException e) {
+                //Nothing entered, return default
+            } catch (NullPointerException e) {
+                //Cancel pressed, let "while" reiterate
+            }
+        } while (resp ==null); //Cancel has been pressed, reiterate
+        return r;
+    }
+
+    public String getDirector() {
+        return director;
+    }
+
+    public void setDirector(String director) {
+        this.director = director;
+    }
+
+    public double getPrice() {
+        return price;
+    }
+
+    public void setPrice(double price) {
+        this.price = price;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+}