Tug As Chapter 4
Tug As Chapter 4
and copyright date.Define a Book constructor to accept and initialize this data. * Include setter and getter methods for all instance data. * Include atoString method that returns a nicely formatted, multi-linedescription of the book. * Create a driver class called Bookshelfwhose main method instantiates and updates, several Bookobjects. */ package Soal1;
public class Book { private String title, author, publisher; private int copyRightDate;
public Book(String bookTitle, String authorName, String publisherName,int date) { title = bookTitle; author = authorName; publisher = publisherName; copyRightDate = date;
public String toString() { return ( "Title + "Author + "Publisher : "+title +"\n" : "+author + "\n" : "+publisher + "\n"
/* * Design and implement a class called Book that containsinstance data for * the title, author, publisher, and copyright date.Define a Book constructor to accept and initialize this data. * Include setter and getter methods for all instance data. * Include atoString method that returns a nicely formatted, multi-linedescription of the book. * Create a driver class called Bookshelfwhose main method instantiates and updates, several Bookobjects. */ package Soal1;
import java.util.Scanner;
public class Bookshelf { public static void main(String[] args) { int pil,pil1,pil2,tahun; String judul,pengarang,penerbit; Scanner scan = new Scanner(System.in);
// creates Book object Book name1 = new Book("Database Processing", "M Kroenke", "Erlangga", year); Book name2 = new Book("Menguasai JAVA 2", "Benny H", "Andi", year2); System.out.println(name1); System.out.println(name2);
do { System.out.print("Select books to be updated ?\n1. Book1\n2. Book2\nYour Choice : "); pil1=scan.nextInt(); System.out.print("Select the data to be updated ?\n1. Title\n2. Author\n3. Publisher\n4. Copyright Date\nYour Choice : "); pil2=scan.nextInt(); switch(pil2) { case 1: { System.out.print("New Title judul=scan.next(); if(pil1==1) { name1.setTitle(judul); } else if(pil1==2) { name2.setTitle(judul); } :");
break; }
case 2: { System.out.print("New Author pengarang=scan.next(); if(pil1==1) { name1.setAuthor(pengarang); } else if(pil1==2) { name2.setAuthor(pengarang); } : ");
break; }
break; }
case 4: { System.out.print("New Copyright Date : "); tahun=scan.nextInt(); if(pil1==1) { name1.setCopyRightDate(tahun); } else if(pil1==2) { name2.setCopyRightDate(tahun); }
} while(pil==1);
SOAL 2 /* * Design and implement a class called Bulb that represents a light bulb that can be turned on and off. * Create a driver class called Lights whose main method instantiates and turns on some Bulb objects. */ package Soal2;
/* * Design and implement a class called Bulb that represents a light bulb that can be turned on and off. * Create a driver class called Lights whose main method instantiates and turns on some Bulb objects. */ package Soal2;
public class Lights { public static void main(String[] args) { Bulb bulb1 = new Bulb (true); //kondisi awal lampu 1 : Hidup Bulb bulb2 = new Bulb (false); //kondisi awal lampu 2 : Mati
System.out.println ("Kondisi lampu 1 : " +bulb1.getLight()); System.out.println ("Kondisi lampu 2 : " +bulb2.getLight());
bulb1.turnOff(); //method mengubah kondisi lampu 1 mati bulb2.turnOn(); //method mengubah kondisi lampu 2 hidup System.out.println("----------------------------------------------------"); System.out.println ("Kondisi lampu 1 sekarang : " +bulb1.getLight()); System.out.println ("Kondisi lampu 2 sekarang : " +bulb2.getLight());
} }
SOAL 3 /* * Design and implement a class called Building that represents a graphical depiction of a building. * Allow the parameters to the constructor to specify the buildings width and height. * Each building should be colored black and should contain a few random windows of yellow. * Create a program that draws a random skyline of buildings. */ package Soal3;
public class Building extends Applet { public void paint(Graphics g) { setSize(700, 400); setBackground(Color.BLUE); g.setColor(Color.GREEN); g.fillRect(0, 300, 700, 100); g.setColor(Color.YELLOW);
int x; Random generator = new Random(); x = generator.nextInt(20)+5; while (x < 500) { int lebar = 30 + (int) (25 * Math.random()); int tinggi = 60 + (int) (75 * Math.random()); BuildingPanel building = new BuildingPanel(lebar, tinggi);
/* * Design and implement a class called Building that represents a graphical depiction of a building. * Allow the parameters to the constructor to specify the buildings width and height. * Each building should be colored black and should contain a few random windows of yellow. * Create a program that draws a random skyline of buildings. */ package Soal3;
import java.awt.*;
private void jendela(Graphics g, int x, int y) //jendela kuning { g.setColor(Color.YELLOW); int nLimit = randomRange(4, 10);
for(int i = 0; i < nLimit; i++) { int xw = randomRange(x, x + lebar-5); int yw = randomRange(y, y + tinggi-9); g.fillRect(xw, yw-tinggi,5, 5); } }
int randomRange(int lo, int hi) { int range = hi - lo + 1; return lo + (int) (range * Math.random()); } }
SOAL 4 /* * Design and implement an application that displays a button and a label. * Every time the button is pushed, the label should display a random number between 1 and 100, inclusive. */ package Soal4;
import javax.swing.JFrame; public class Random { public static void main(String[] args) { JFrame frame = new JFrame ("Building"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack(); frame.setVisible(true);
} }
/* * Design and implement an application that displays a button and a label. * Every time the button is pushed, the label should display a random number between 1 and 100, inclusive. */ package Soal4;
public class RandomPanel extends JPanel { private JButton randButton; private JLabel label; private String name = "???";
public RandomPanel() { JPanel primary = new JPanel(); randButton = new JButton("Click Me...");
class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { label.setText(new Integer(new Random().nextInt(100) + 1).toString()); }
} }
SOAL 5 /* * Design and implement an application that draws a traffic light and uses a push button * to change the state of the light derive the drawing surface from the Jpanel class * and use another panel to organize the drawing surface of the button */ package Soal5;
import javax.swing.JFrame;
public class TrafficLight { public static void main (String[] args) { JFrame frame=new JFrame("Traffic Light"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); TrafficLightPanel panel = new TrafficLightPanel();
/* * Design and implement an application that draws a traffic light and uses a push button * to change the state of the light derive the drawing surface from the Jpanel class * and use another panel to organize the drawing surface of the button */ package Soal5;
public class TrafficLightPanel extends JFrame implements MouseListener { private JButton Start = new JButton("Press"); private JPanel JGreen = new JPanel(); private JPanel JYellow = new JPanel(); private JPanel JRed = new JPanel(); private JLabel Trafic = new JLabel("Traffic Light",JLabel.CENTER); private JLabel Mouse = new JLabel("",JLabel.CENTER);
public TrafficLightPanel() {
setLayout(null);
Start.setBounds(50,45,80,20); Start.addMouseListener(this);
@Override public void mouseReleased(MouseEvent me) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }
SOAL 6 /* * Develop an application that implements a prototype user interface for composing an e-mail message. * The application should have text
elds for the To, CC, and Bcc address lists and subject line, * and one for the message body. Include a button labeled Send. When the Send Button is pushed, * the program should print the contents of all
import javax.swing.JFrame;
public class Email { public static void main(String[] args) { EmailPanel panel = new EmailPanel(); panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel.setTitle("EMAIL"); panel.setSize(530, 400); panel.setVisible(true); } }
/* * Develop an application that implements a prototype user interface for composing an e-mail message. * The application should have text
elds for the To, CC, and Bcc address lists and subject line, * and one for the message body. Include a button labeled Send. When the Send Button is pushed, * the program should print the contents of all
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.JTextField;
public class EmailPanel extends JFrame { JLabel to = new JLabel(); JLabel cc = new JLabel(); JLabel bcc = new JLabel(); JLabel subject = new JLabel(); JTextField tfto = new JTextField(); JTextField tfcc = new JTextField();
JTextField tfbcc = new JTextField(); JTextField tfsubject = new JTextField(); JTextArea tArea = new JTextArea(); JButton button = new JButton();
public EmailPanel() { this.getContentPane().setLayout(null); to.setFont(new java.awt.Font("Arial",Font.ITALIC,15)); to.setText("Kepada:"); to.setBounds(15, 30, 60, 30);
tfto.setBounds(80, 30, 400, 30); tfcc.setBounds(80, 60, 400, 30); tfbcc.setBounds(80, 90, 400, 30);
this.getContentPane().add(button); this.getContentPane().add(tArea); this.getContentPane().add(tfto); this.getContentPane().add(tfcc); this.getContentPane().add(tfbcc); this.getContentPane().add(tfsubject); this.getContentPane().add(subject); this.getContentPane().add(bcc); this.getContentPane().add(cc); this.getContentPane().add(to); }
private class buttonListener implements ActionListener { public void actionPerformed (ActionEvent event) { System.out.println("Kepada System.out.println("Cc System.out.println("Bcc : "+ tfto.getText()+"\n");
System.out.println("Subjek System.out.println("Pesan
System.out.println(tArea.getText()); } } }