Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tanggal 12 Mei 2016
- Nama Kelompok :1. Hermawan Nugroho (A11.2014.08231)
- 2. Rangga Aditya W. (A11.2014.08233)
- 3. Julia Dwi K. (A11.2014.08238)
- 4. Digi Legista (A11.2014.08244)
- "SOAL 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)//tidak ada gunanya tetapi jika di hapus akan error
- {
- }
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- ===InputUmur2.java===
- import javax.swing.*;
- import java.util.*;
- class InputUmur2
- {
- private static final String DEFAULT_MESSAGE = "umur kamu : ";
- 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);
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- ===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;
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- "SOAL 2"
- ===TextFrameTugas===
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- class TextFrameTugas extends JFrame implements ActionListener
- {
- private static final int FRAME_WIDTH = 400;
- private static final int FRAME_HEIGHT = 400;
- private static final int FRAME_X_ORIGIN = 250;
- private static final int FRAME_Y_ORIGIN = 150;
- private JButton tampil;
- private JTextField input;
- private JLabel output;
- private JLabel prompt;
- private JLabel image;
- 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);
- setLocation(FRAME_X_ORIGIN,FRAME_Y_ORIGIN);
- contentPane.setLayout(new FlowLayout());
- image = new JLabel(new ImageIcon("4X6.jpg"));
- image.setSize(50,50);
- contentPane.add(image);
- prompt = new JLabel();
- prompt.setText("Ketikkan namamu");
- contentPane.add(prompt);
- add(contentPane);
- input = new JTextField("",25);
- contentPane.add(input);
- tampil = new JButton("tampil");
- contentPane.add(tampil);
- output = new JLabel("");
- contentPane.add(output);
- tampil.addActionListener(this);
- }
- public void actionPerformed(ActionEvent event)
- {
- output.setText(input.getText());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement