Date post: | 08-Nov-2014 |
Category: |
Documents |
Upload: | joy-sipahutar |
View: | 234 times |
Download: | 0 times |
SOAL 1
/*
* 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;
/**
*
* @author Joy Salomo 1108605033
*/
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 void setAuthor(String authorName)
{
author = authorName;
}
public String getAuthor()
{
return author;
}
public void setTitle(String bookTitle)
{
title = bookTitle;
}
public String getTitle()
{
return title;
}
public void setPublisher(String publisherName)
{
publisher = publisherName;
}
public String getPublisher()
{
return publisher;
}
public void setCopyRightDate(int date)
{
copyRightDate = date;
}
public int getCopyRightDate()
{
return copyRightDate;
}
public String toString()
{
return ( "Title : "+title +"\n"
+ "Author : "+author + "\n"
+ "Publisher : "+publisher + "\n"
+ "Copyright Date : "+copyRightDate+"\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;
/**
*
* @author Joy Salomo 1108605033
*/
public class Bookshelf
{
public static void main(String[] args)
{
int pil,pil1,pil2,tahun;
String judul,pengarang,penerbit;
Scanner scan = new Scanner(System.in);
int year = 2003;
int year2 = 2004;
// 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;
}
case 3:
{
System.out.print("New Publisher : ");
penerbit=scan.next();
if(pil1==1)
{
name1.setPublisher(penerbit);
}
else if(pil1==2)
{
name2.setPublisher(penerbit);
}
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);
}
break;
}
default:
{
System.out.print("Error !");
break;
}
}
System.out.print("\nContinue Updating ?\n1. Yes\n2. No\nYour Choice : ");
pil=scan.nextInt();
} while(pil==1);
System.out.println("\nUpdated : ");
System.out.println(name1);
System.out.println(name2);
}
}
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;
/**
*
* @author Joy Salomo 1108605033
*/
public class Bulb
{
private boolean isLit;
public Bulb (boolean light)
{
isLit = light;
}
public void turnOn()
{
isLit = true;
}
public void turnOff()
{
isLit = false;
}
public String getLight ()
{
if(isLit)
{
return "On";
}
else
{
return "Off";
}
}
}
/*
* 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;
/**
*
* @author Joy Salomo 1108605033
*/
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 building’s 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;
/**
*
* @author Joy Salomo 1108605033
*/
import java.applet.Applet;
import java.awt.*;
import java.util.Random;
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);
g.fillOval(20,20, 80, 80);
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);
building.draw(g, x, 300);
x = x + lebar + 15;
}
}
}
/*
* Design and implement a class called Building that represents a graphical depiction of a building.
* Allow the parameters to the constructor to specify the building’s 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;
/**
*
* @author Joy Salomo 1108605033
*/
import java.awt.*;
public class BuildingPanel
{
private int lebar;
private int tinggi;
BuildingPanel(int lebar, int tinggi)
{
this.lebar = lebar;
this.tinggi = tinggi;
}
public void draw(Graphics g, int x, int y) //bangunan hitam
{
g.setColor(Color.BLACK);
g.fillRect(x, y - tinggi, lebar, tinggi);
jendela(g, x, y);
}
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;
/**
*
* @author Joy Salomo 1108605033
*/
import javax.swing.JFrame;
public class Random
{
public static void main(String[] args)
{
JFrame frame = new JFrame ("Building");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RandomPanel panel = new RandomPanel();
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;
/**
*
* @author Joy Salomo 1108605033
*/
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
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...");
ButtonListener listener = new ButtonListener();
randButton.addActionListener(listener);
label = new JLabel(name);
setBackground(Color.GREEN);
add(label);
add(randButton);
}
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;
/**
*
* @author Joy Salomo 1108605033
*/
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();
frame.getContentPane().add(new TrafficLightPanel());
//frame.pack();
frame.setVisible(true);
}
}
/*
* 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;
/**
*
* @author Joy Salomo 1108605033
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
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);
add(Start); add(JGreen); add(JRed); add(Trafic); add(JYellow); add(Mouse);
Trafic.setBounds(50,0,80,20);
Mouse.setBounds(40,280,100,20);
Start.setBounds(50,45,80,20);
Start.addMouseListener(this);
JRed.setBounds(60,70,60,60);
JRed.setBackground(Color.RED);
//JRed.setVisible(true);
JYellow.setBounds(60,140,60,60);
JYellow.setBackground(Color.GRAY);
//JYellow.setVisible(false);
JGreen.setBounds(60,210,60,60);
JGreen.setBackground(Color.GRAY);
//JGreen.setVisible(false);
setVisible(true);
setSize(200,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
show();
}
public void mouseClicked(MouseEvent e)
{
if(e.getSource()==Start)
{
JRed.setBackground(Color.GRAY);
JYellow.setBackground(Color.YELLOW);
JGreen.setBackground(Color.GRAY);
Mouse.setText("Ready");
}
}
public void mouseEntered(MouseEvent e)
{
if(e.getSource()==Start)
{
JRed.setBackground(Color.GRAY);
JYellow.setBackground(Color.YELLOW);
JGreen.setBackground(Color.GRAY);
Mouse.setText("Ready");
}
}
public void mouseExited(MouseEvent e)
{
if(e.getSource()==Start)
{
JRed.setBackground(Color.RED);
JYellow.setBackground(Color.GRAY);
JGreen.setBackground(Color.GRAY);
Mouse.setText("Stop");
}
}
public void mousePressed(MouseEvent e)
{
if(e.getSource()==Start)
{
JRed.setBackground(Color.GRAY);
JYellow.setBackground(Color.GRAY);
JGreen.setBackground(Color.GREEN);
Mouse.setText("Go");
}
}
@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
elds to standard output using println statements.
*/
package Soal6;
/**
*
* @author Joy Salomo 1108605033
*/
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
elds to standard output using println statements.
*/
package Soal6;
/**
*
* @author Joy Salomo 1108605033
*/
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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);
cc.setFont(new java.awt.Font("Arial",Font.ITALIC,15));
cc.setText("Cc:");
cc.setBounds(52, 60, 40, 20);
bcc.setFont(new java.awt.Font("Arial",Font.ITALIC,15));
bcc.setText("Bcc:");
bcc.setBounds(44, 90, 40, 20);
subject.setFont(new java.awt.Font("Arial",Font.ITALIC,15));
subject.setText("Subjek:");
subject.setBounds(16, 120, 100, 20);
tfto.setBounds(80, 30, 400, 30);
tfcc.setBounds(80, 60, 400, 30);
tfbcc.setBounds(80, 90, 400, 30);
tfsubject.setBounds(80, 120, 400, 30);
tArea.setLineWrap(true);
tArea.setBounds(20, 185, 460, 130);
button.setText("Kirim");
button.setBounds(400, 320, 80, 30);
button.addActionListener(new buttonListener());
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 : "+ tfto.getText()+"\n");
System.out.println("Cc : "+ tfcc.getText()+"\n");
System.out.println("Bcc : "+ tfbcc.getText()+"\n");
System.out.println("Subjek : "+ tfsubject.getText()+"\n");
System.out.println("Pesan : \n");
System.out.println(tArea.getText());
}
}
}