aawangg

Tugas Modul VII

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