You are on page 1of 5

public class averageNumber { public static void main (String[] args) { int score = 0; int sum = 1; int count

= -1; double average; while (score >= 0) { count++; sum = sum + score; } sum = sum - score - 1; System.out.println("count: " + count + ", sum: " + sum); average = (double)sum/(double)count; System.out.println("average: " + average); } // end of main }

import javax.swing.JOptionPane; import java.util.Scanner; public class MathWeek1 { public static void main (String[] args) { int num1, num2, num3; int sum, product, data; double average; int smallest = Integer.MAX_VALUE; String input; input = JOptionPane.showInputDialog("Please enter an integer."); num1 = Integer.parseInt(input); input = JOptionPane.showInputDialog("Please enter another different integer."); num2 = Integer.parseInt(input);

input = JOptionPane.showInputDialog("Please enter one last integer."); num3 = Integer.parseInt(input); JOptionPane.showMessageDialog(null, "Your numbers were: " + num1 + "," + num2 + "," + num3); sum = (num1 + num2 + num3); JOptionPane.showMessageDialog(null, "The sum is: " + sum); product = (num1 * num2 * num3); JOptionPane.showMessageDialog(null, "The product is: " + product); average = (sum / 3); JOptionPane.showMessageDialog(null, "The average is: " + average); { data = Integer.parseInt(input); if (data < smallest) smallest = data; } JOptionPane.showMessageDialog(null,"The smallest value is " + smallest); System.exit(0); } }

Scanner input = new Scanner( System.in ); double input_num; // declaration double output_num; System.out.print( "Enter input number: " ); // "input_num" is declared as type double // so you will have trouble if you try to read // a double with the nextInt method. Use // nextDouble input_num = input.nextDouble(); output_num = (input_num*1.5/2048)-0.80; // You want to print type double here // so you must specify a floating point format (%f) // instead of integer format (%d).

System.out.printf( "Output is %.3f\n", output_num );

1. Create AWT Label Example 2. This java example shows how to create a label using AWT Label class. 3. */

4.

5. 6.

7.
8. 9. 10. 11.

import java.applet.Applet; import java.awt.Label; /* <applet code="CreateLabel" width=200 height=200> </applet> */ public class CreateLabel extends Applet{ public void init(){ /* * To create a new label use * Label() constructor of AWT Label class. */ Label label1 = new Label(); /* * To set label text use, * void setText(String text) method of AWT Label */

12.

13. 15.
18. 19. 20.

14. 16.

17.

21.

22. 24.

23.
25. 26. class 27.

28.

29. 31.
32. 33. 34.

label1.setText("My Label 1");


/* * To create label with the text use * Label(String text) constructor. */ Label label2 = new Label("My Label 2"); /* * To add label use * void add(Component c) method. */

30.

35.

36. 38.
39. 40. 41.

37.

42.

43. 44. 46.


47.

add(label1); add(label2);
} }

45.

1. 2. 3. AWT. 4.

/* Create Frame Window Example This java example shows how to create frame window using Java */ import java.awt.Frame; /* * To create a stand alone window, class should be extended from * Frame and not from Applet class. */ public class CreateFrameWindowExample extends Frame{

5.

6.

7.
8. 9. 10. 11.

12.
13.

14. 16. 17. 19. 20. 22.

15.

CreateFrameWindowExample(String title){
//call the superclass constructor super(); //set window title using setTitle method this.setTitle(title); /* * Newly created window will not be displayed until * setVisible(true). */ this.setVisible(true); } public static void main(String args[]){

18.

21.
23. we call 24. 25.

26. 27. 29. 30. 31. 33.

28. CreateFrameWindowExample window =


new

CreateFrameWindowExample("Create Window Example"); 32.


34. handle the events 35. 36. 37. you will have 38. a terminal window 39. 40. 41. } /* * In order to close the window, we will have to * and call setVisible(false) method. * * This Example program does not handle events, so * to terminate the program (by pressing ctrl+C) in * to close the frame window. */

1.
2.

import java.util.Scanner; // import Scanner

3.
4.

5.

6.

7.

public class Average { public static void main(String args[]) { Scanner input = new Scanner(System.in); int x; // first integer int y; // second integer int z; // third integer System.out.print("Enter first integer: "); x = input.nextInt(); System.out.print("Enter second integer: "); y = input.nextInt(); System.out.print("Enter third integer: "); z = input.nextInt(); result = (x + y + z); System.out.printf("The sum of three integers is %d\n", result); result = (x + y + z)/3; System.out.printf("The average is %d\n", result);

8.

9. 10. 11.
12. 14. 15. 17. 18. 20. 21. 22. 24.

13. 16. 19.

23. 25. 26.


27. 28.

result = (x * y * z); 29. System.out.printf("The product of three integers is %d\n", result); 30. result = (x - y - z); 31. System.out.printf("The difference of three integers is %d \n", result); 32. 33. if (x <= y && x <= z) 34. System.out.printf("The smallest integer is %d\n", result); 35. if (y <= x && y <= z) 36. System.out.printf("The smallest integer is %d\n", result); 37. if (z <= x && z <= y) 38. System.out.printf("The smallest integer is %d\n", result); 39. } // end method main 40. 41. } // end class Average

You might also like