Advertisement
Guest User

TUGAS PBO VII

a guest
May 26th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. tanggal 12 Mei 2016
  2. Nama Kelompok :1. Hermawan Nugroho (A11.2014.08231)
  3. 2. Rangga Aditya W. (A11.2014.08233)
  4. 3. Julia Dwi K. (A11.2014.08238)
  5. 4. Digi Legista (A11.2014.08244)
  6.  
  7. "SOAL 1"
  8.  
  9. ===InputUmurMain.java===
  10.  
  11. public class InputUmurMain
  12. {
  13. private static int age;
  14. public static void main (String[] args)
  15. {
  16. try
  17. {
  18. InputUmur2 umur = new InputUmur2();
  19. age=umur.getAge();
  20.  
  21. }
  22. catch(InputUmurException e)
  23. {
  24. System.out.println(e);
  25. }
  26. catch(Exception e)//tidak ada gunanya tetapi jika di hapus akan error
  27. {
  28.  
  29. }
  30.  
  31. }
  32. }
  33. ---------------------------------------------------------------------------------------------------------------------------------------
  34.  
  35. ===InputUmur2.java===
  36.  
  37. import javax.swing.*;
  38. import java.util.*;
  39.  
  40.  
  41. class InputUmur2
  42. {
  43. private static final String DEFAULT_MESSAGE = "umur kamu : ";
  44. private static final int DEFAULT_LOWER_BOUND = 0;
  45. private static final int DEFAULT_UPPER_BOUND = 100;
  46. private int lowerBound;
  47. private int upperBound;
  48. private Scanner scanner;
  49.  
  50. public InputUmur2()
  51. {
  52. init(DEFAULT_LOWER_BOUND, DEFAULT_UPPER_BOUND);
  53. }
  54. public InputUmur2(int low, int high) throws IllegalArgumentException
  55. {
  56. if(low>high)
  57. {
  58. throw new IllegalArgumentException( " ("+low+") lebih besar dari ("+high+") ");
  59. }
  60. else
  61. {
  62. init(low,high);
  63. }
  64. }
  65. public int getAge() throws Exception
  66. {
  67. return getAge(DEFAULT_MESSAGE);
  68. }
  69.  
  70. public int getAge(String prompt) throws InputUmurException
  71. {
  72. int age;
  73. while(true)
  74. {
  75. System.out.print(prompt);
  76. try
  77. {
  78. age=scanner.nextInt();
  79. if(age< lowerBound|| age>upperBound)
  80. {
  81. throw new InputUmurException ("Input out of bound",lowerBound,upperBound,age);
  82. }
  83. return age;
  84. }
  85. catch(InputMismatchException e)
  86. {
  87. scanner.next();
  88. System.out.println("Input Salah. \n" + "Input harus dalam bentuk bilangan");
  89. }
  90. }
  91. }
  92. private void init (int low, int high)
  93. {
  94. lowerBound = low;
  95. upperBound = high;
  96. scanner = new Scanner(System.in);
  97. }
  98. }
  99. ---------------------------------------------------------------------------------------------------------------------------------------
  100.  
  101. ===InputUmurException.java===
  102.  
  103. class InputUmurException extends Exception
  104. {
  105. private static final String DEFAULT_MESSAGE = "Input out of bounds";
  106. private int lowerBound;
  107. private int upperBound;
  108. private int value;
  109.  
  110. public InputUmurException (int low, int high, int input)
  111. {
  112. this(DEFAULT_MESSAGE, low, high,input);
  113. }
  114. public InputUmurException (String msg, int low, int high, int input)
  115. {
  116. super(msg);
  117. if(low>high)
  118. {
  119. throw new IllegalArgumentException();
  120. }
  121. lowerBound = low;
  122. upperBound = high;
  123. value = input;
  124. }
  125. public int lowerBound()
  126. {
  127. return lowerBound;
  128. }
  129. public int upperBound()
  130. {
  131. return upperBound;
  132. }
  133. public int value()
  134. {
  135. return value;
  136. }
  137. }
  138. ---------------------------------------------------------------------------------------------------------------------------------------
  139.  
  140. "SOAL 2"
  141. ===TextFrameTugas===
  142.  
  143. import javax.swing.*;
  144. import java.awt.*;
  145. import java.awt.event.*;
  146.  
  147.  
  148. class TextFrameTugas extends JFrame implements ActionListener
  149. {
  150. private static final int FRAME_WIDTH = 400;
  151. private static final int FRAME_HEIGHT = 400;
  152.  
  153. private static final int FRAME_X_ORIGIN = 250;
  154. private static final int FRAME_Y_ORIGIN = 150;
  155.  
  156.  
  157. private JButton tampil;
  158. private JTextField input;
  159. private JLabel output;
  160.  
  161. private JLabel prompt;
  162. private JLabel image;
  163.  
  164.  
  165.  
  166. public static void main (String[] args)
  167. {
  168. TextFrameTugas frame = new TextFrameTugas();
  169. frame.setVisible(true);
  170. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  171. }
  172.  
  173. private TextFrameTugas ()
  174. {
  175. Container contentPane = new Container();
  176. setSize(FRAME_WIDTH ,FRAME_HEIGHT);
  177. setLocation(FRAME_X_ORIGIN,FRAME_Y_ORIGIN);
  178. contentPane.setLayout(new FlowLayout());
  179. image = new JLabel(new ImageIcon("4X6.jpg"));
  180. image.setSize(50,50);
  181. contentPane.add(image);
  182. prompt = new JLabel();
  183. prompt.setText("Ketikkan namamu");
  184. contentPane.add(prompt);
  185. add(contentPane);
  186. input = new JTextField("",25);
  187. contentPane.add(input);
  188. tampil = new JButton("tampil");
  189. contentPane.add(tampil);
  190. output = new JLabel("");
  191. contentPane.add(output);
  192. tampil.addActionListener(this);
  193.  
  194. }
  195.  
  196. public void actionPerformed(ActionEvent event)
  197. {
  198. output.setText(input.getText());
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement