Java Programming 1 P2, H2

Java Programming 1 P2, H2.

Project 2

 

Description:

Using GUIGreet.java as a model, create a Java stand-alone GUI program that displays a Fahrenheit to Celsius temperature conversion table in an AWT Frame or a swing JFrame.  The table should run from 0 degrees Fahrenheit to 250 degrees Fahrenheit, in steps of 10 degrees.  Round the Celsius temperature values to the nearest integer.

Display any temperatures below the freezing point of water (32o Fahrenheit) in blue and any temperatures above the boiling point of water (212o Fahrenheit) in red (the rest in black).

The formula to use is:

Degrees Celsius = ( 5 ÷ 9 ) × ( Degrees Fahrenheit – 32 Degrees )

See TempConv.jar model solution for a sample chart; just download and double-click to run.

Other Requirements:

You must turn in a stand-alone AWT or swing program, not an Applet or JApplet nor a JavaFx program.  You must meet all the requirements from the description above.  If you include any creative extras, be sure your program still performs the basic chart as described above.  Creative extras are extras, and you are not free to modify the project requirements.

It is not acceptable to pre-compute each Celsius value, and just have many “g.drawString()” statements.  You need to use Java to calculate each value using the correct formula, and use various control structures.

GUI Greet Java

 

// A stand-alone GUI Java program to display a friendly greeting.
 2: // Also added code to close the application when the user clicks
 3: // the mouse in the close box.
 4: 
 5: // Written by Wayne Pollock, Tampa, FL USA, 1999
 6: 
 7: import java.awt.*;
 8: import java.awt.event.*;
 9: 
10: public class GUIGreet extends Frame
11: {
12:    private String message = "Hello, World!";
13: 
14:    public GUIGreet ()
15:    {
16:       setTitle( "A Friendly Greeting" );
17:       setSize( 300, 200 );
18:       setVisible( true );
19: 
20:       addWindowListener(
21:          new WindowAdapter()
22:          {  public void windowClosing( WindowEvent e )
23:             {  System.exit( 0 );
24:             }
25:          }
26:       );
27:    }
28: 
29:    public static void main ( String [] args )
30:    {
31:       GUIGreet me = new GUIGreet();
32:    }
33: 
34:    public void paint ( Graphics g )
35:    {
36:       g.setColor( Color.RED );
37:       g.drawRect( 30, 40, 240, 130 );
38:       g.setColor( Color.BLUE );
39:       g.setFont( new Font( "SansSerif", Font.BOLD, 24 ) );
40:       g.drawString( message, 70, 110 );  // Position determined
41:    }                                     // by trial and error!
42: }

Homework 2

 

  1. Can the following three conversions involving casting be allowed?  If so, find the converted result.    boolean b = true;     int i = (int) b;      int i = 1;     boolean b = (boolean) i;      int i = 1;     String s = (String) i;
  2. What is the printout of the code in (a) and (b): if number is 30?  If number is 35?
    1.     if (number % 2 == 0)            System.out.println(number + ” is even.”);            System.out.println(number + ” is odd.”);
    2.     if (number % 2 == 0)            System.out.println(number + ” is even.”);         else            System.out.println(number + ” is odd.”);
  3. How do you generate a random integer i such that
    1. 0 ≤ i < 20?
    2. 10 ≤ i < 20?
    3. 10 ≤ i ≤ 50?
  4. What is the value of the expression:  (ch >= 'A' && ch <= 'Z')
    1. if ch is 'A'?
    2. if ch is 'p'?
    3. if ch is 'E'?
    4. if ch is '5'?
  5. Write a Boolean expression that evaluates true if weight is greater than 50 pounds or height is greater than 60 inches.
  6. Write a switch statement that assigns to a String variable dayName with Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday, if the int variable day is 0, 1, 2, 3, 4, 5, and 6, accordingly.
  7. Evaluate the following two expressions:      2 * 2 – 3 > 2 && 4 – 2 > 5        2 * 2 – 3 > 2 || 4 – 2 > 5
  8. Do the following two loops result in the same value in sum?
    1. 1:      int sum = 0; 2:      for ( int i = 0; i < 10; ++i ) { 3:          sum += i; 4:      }
    2. 1:      int sum = 0; 2:      for ( int i = 0; i < 10; i++ ) { 3:          sum += i; 4:      }
  9. If a variable is declared in the for loop control, can it be used after the loop exits?
  10. The for loop on the left is converted into the while loop on the right.  What is wrong?  Correct it.1: int sum = 0; 2: for (int i = 1; i <= 4; i++) { 3:    if (i % 3 == 0)  continue; 4:    sum += i; 5: }1: int i = 1, sum = 0; 2: while (i <= 4) { 3:    if (i % 3 == 0)  continue; 4:    sum += i; 5:    i++; 6: }
    1. After the break statement is executed in the following loop, which statement is executed?  Show the output.1: for (int i = 1; i < 4; i++) { 2:    for (int j = 1; j < 4; j++) { 3:       if (i * j > 2) 4:          break; 5:          System.out.println(i * j); 6:       } 7:       System.out.println(i); 8: }
    2. After the continue statement is executed in the following loop, which statement is executed?  Show the output.1: for (int i = 1; i < 4; i++) { 2:    for (int j = 1; j < 4; j++) { 3:       if (i * j > 2) 4:          continue; 5:          System.out.println(i * j); 6:       } 7:    System.out.println(i); 8: }
  11. Write a Java regular expression (“regex”), that matches en_US (standard Americian) formatted whole numbers.  Such numbers are composed of only digits and commas.  For example:InputResult From
    Correct Regex,0False,False1,20False1,234,False0True12True123True1,234True0,000True100,000True
  • attachment

    TempConv.jar

Java Programming 1 P2, H2