Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Tugas Modul VII
- Jum'at, 13 Mei 2016
- Kelompok
- Mahatma Adi G - A11.2014.08570
- Mahesa Ratsilatama - A11.2014.08592
- Ghina Adiyaksa Y - A11.2014.08556
- Nugraha Bayu Aji - A11.2014.08525
- -------------------------------------------------------------------------------------------------------
- === Tugas 1 ===
- //InputUmurMain.java\\
- public class InputUmurMain
- {
- private static int age;
- public static void main (String[] args)
- {
- try{
- InputUmur2 umur = new InputUmur2();
- age=umur.getAge();
- }catch(InputUmurException e)
- {
- System.out.println(e);
- }catch(Exception e)
- {
- System.out.println ("ups salah");
- }
- }
- }
- --------------------------------------------------------------------------------------------------------
- // InputUmurException.java \\
- class InputUmurException extends Exception
- {
- private static final String DEFAULT_MESSAGE = "Input out of bounds";
- private int lowerBound;
- private int upperBound;
- private int value;
- public InputUmurException (int low, int high, int input)
- {
- this(DEFAULT_MESSAGE, low, high,input);
- }
- public InputUmurException (String msg, int low, int high, int input)
- {
- super(msg);
- if(low>high)
- {
- throw new IllegalArgumentException();
- }
- lowerBound = low;
- upperBound = high;
- value = input;
- }
- public int lowerBound()
- {
- return lowerBound;
- }
- public int upperBound()
- {
- return upperBound;
- }
- public int value()
- {
- return value;
- }
- }
- -------------------------------------------------------------------------------------------------------
- //InputUmur2.java\\
- import javax.swing.*;
- import java.util.*;
- class InputUmur2
- {
- private static final String DEFAULT_MESSAGE = "Umur mu : ";
- private static final int DEFAULT_LOWER_BOUND = 0;
- private static final int DEFAULT_UPPER_BOUND = 100;
- private int lowerBound;
- private int upperBound;
- private Scanner scanner;
- public InputUmur2()
- {
- init(DEFAULT_LOWER_BOUND, DEFAULT_UPPER_BOUND);
- }
- public InputUmur2(int low, int high) throws IllegalArgumentException
- {
- if(low>high)
- {
- throw new IllegalArgumentException( " ("+low+") lebih besar dari ("+high+") ");
- }else{
- init(low,high);
- }
- }
- public int getAge() throws Exception{
- return getAge(DEFAULT_MESSAGE);
- }
- public int getAge(String prompt) throws InputUmurException{
- int age;
- while(true){
- System.out.print(prompt);
- try{
- age=scanner.nextInt();
- if(age< lowerBound|| age>upperBound)
- {
- throw new InputUmurException ("Input out of bound",lowerBound,upperBound,age);
- }
- return age;
- }catch(InputMismatchException e)
- {
- scanner.next();
- System.out.println("Input Salah. \n" + "Input harus dalam bentuk bilangan");
- }
- }
- }
- private void init (int low, int high)
- {
- lowerBound = low;
- upperBound = high;
- scanner = new Scanner(System.in);
- }
- }
- ------------------------------------------------------------------------------------------------------
- === Tugas 2 ===
- //TextFrameTugas.java\\
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- class TextFrameTugas extends JFrame implements ActionListener
- {
- private static final int FRAME_HEIGHT=400;
- private static final int FRAME_WIDTH=400;
- private static final int FRAME_X_ORIGIN = 250;
- private static final int FRAME_Y_ORIGIN = 150;
- private JLabel prompt;
- private JLabel image;
- private JButton cetak_nama;
- private JTextField input_nama;
- private JLabel output;
- public static void main (String[] args)
- {
- TextFrameTugas frame = new TextFrameTugas();
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- private TextFrameTugas ()
- {
- Container contentPane = new Container();
- setSize(FRAME_WIDTH ,FRAME_HEIGHT);
- setResizable(false);
- setTitle("Program button sederhana");
- setLocation(FRAME_X_ORIGIN,FRAME_Y_ORIGIN);
- contentPane.setLayout(new FlowLayout());
- image = new JLabel(new ImageIcon("dol.gif"));
- image.setSize(50,50);
- contentPane.add(image);
- prompt = new JLabel();
- prompt.setText("Ketikkan namamu");
- prompt.setSize(150, 25);
- contentPane.add(prompt);
- add(contentPane);
- input_nama = new JTextField("",11);
- input_nama.setSize(150,20);
- contentPane.add(input_nama);
- cetak_nama = new JButton("Cetak");
- cetak_nama.setSize(80,20);
- contentPane.add(cetak_nama);
- output = new JLabel("");
- output.setSize(150,25);
- contentPane.add(output);
- cetak_nama.addActionListener(this);
- }
- public void actionPerformed(ActionEvent event)
- {
- output.setText(input_nama.getText());
- //System.out.println(input_nama.getText());
- }
- }
Add Comment
Please, Sign In to add comment