1
Midterm Exam E02 Handout
for
e02
CS56 F16

Code for Amazoony question

Product.java

1
2
3
4
5
6
7
/** something that can be sold */
public abstract class Product {
    
    /** get the price (in cents) */
    public abstract int getPrice();
    
}

Shippable.java

1
2
3
4
5
/** something that can be shipped */
public interface Shippable {
    /** get the shipping weight in pounds */
    public double getWeight();
}

Book.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/** A Book */
public class Book extends Product implements Shippable {

    private int price;
    private double weight;
    private String author;
    private String title;

    public Book(String author, String title, int price,
                double weight) {
        this.author = author;
        this.title = title;
        this.price = price;
        this.weight = weight;
    }

    public int getPrice() {return this.price;}
    public String getTitle() {return this.title;}
    public String getAuthor() {return this.author;}
    public double getWeight() {return this.weight;}
}

Song.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/** A downloadable Song */
public class Song extends Product {

    private int price;
    private String artist;
    private String title;

    public Song(String artist, String title, int price) {
        this.artist = artist;
        this.title = title;
        this.price = price;
    }

    public Song(String artist, String title) {
        this(artist,title,99);
    }

    public int getPrice() {return this.price;}
    public String getTitle() {return this.title;}
    public String getArtist() {return this.artist;}

}

 

ButtonDemo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ButtonDemo {
  private JFrame frame;
  private JLabel label;
  public static void main (String[] args) {
        ButtonDemo gui = new ButtonDemo ();
        gui.go();
  }
  public void go() {
        frame = new JFrame();
        BoxLayout boxLayout = new BoxLayout(frame.getContentPane(), 
                                            BoxLayout.X_AXIS);
        frame.setLayout(boxLayout);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton button1 = new JButton("Button 1");
        button1.addActionListener(new Listener1());
        JButton button2 = new JButton("Button 2");
        button2.addActionListener(new Listener2());
        label = new JLabel("Nothing clicked yet");
        
        frame.getContentPane().add(label);
        frame.getContentPane().add(button1);
        frame.getContentPane().add(button2);
        frame.setSize(400,100);
        frame.setVisible(true);
  }
    
  class Listener1 implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            label.setText("Button 1 clicked");
        }
    }

  class Listener2 implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            label.setText("Button 2 clicked");
        }
    }

}
End of Handout