Total de visualizações de página

sábado, 11 de dezembro de 2010

Java Lab - Examples System.out.println()


Laboratory #2

Part I.  Discovering Java.  Manipulating a visual Java object.

1.    Start DrJava from the Start-Programs Menu.
2.    Maximize the Interaction pane and type the following statements:
a.    Type import javax.swing.*; and press the Enter key.  What happens? ___________
b.    Type JFrame win = new JFrame("Hello") and press Enter.  What happens?_________
c.    Type win.show() and press Enter.  What happens now? _______________________
d.    Type win.setSize(50, 50) and press the Enter key.  What happens? ________________
e.    Type win.setSize(150, 150) What happens? ____________________
f.     Type win.setTitle("your name") What happens? __________________
g.    Type win.move(50, 50)  What happens? _____________________
h.    Change the dimensions of the window.  How do you do that? _____________
i.      Position the window somewhere in the middle of the screen?  How ? ____________
j.      Create another window.
k.    Set its title to Lab2.
l.      Set its size to a square.
m.   Place the window in the top right corner.


Part II.  Discovering some more

3.    In the Interactions pane type the following statements:
·         Type 56 + 23 and press the Enter key.  What happens? ________________________
·         Type 10 / 3 and press Enter.  What happens? ____________________________
·         Type 10.0 / 3.0 and press Enter.  What happens now? _______________________
·         Type 10.0 / 3 and press Enter.  What happens now? _______________________
·         Any conclusions? ____________________________________________________
·         Type System.out.println(“CS 120”) and press the Enter key.  What happens? ____________________________
·         Type System.out.println(“56 + 23”). What happens? ____________________
·         Type System.out.println(56 + 23). What happens? _______why? ___________
·         Use System.out.println() to display 10 / 3.   What happens? _____________________
·         Use System.out.println to display your first name.  How do you do that? _____________

Part III.  Testing the IDE.

4.    Type the Hello.java program shown below.
5.    Save the program in your H: drive folder CS120 subfolder Lab2 as Hello.java.
6.    Click on the Compile All icon to compile the program.  Correct errors until no errors exist.
7.    To Run the program you must call the interpreter in the Interactions panel.  Type java Hello.
8.    The output will be displayed in the Console panel as well as the Interactions.

Part IV.  Our first Simple Java Program.

1.    Click on the New button to start a new Java program. 
2.    Use the Hello program as a template but this time call it BoxName.java and save it.
3.    Write a program that prints your name centered inside a box on the screen, like this:

 






Part V.  Another  Java Program.

1.    Click on the New button to start a new Java program.  Call it Face.java.
2.    Write a program that prints a face.
3.    Look at the keyboard and select the characters that will provide you with a good face.
4.    Use comments to describe the program, write your name and the date.

Note: Show your work before you leave the lab so you can be counted as present!

 

Laboratory #2

Part I.  Discovering Java.  Manipulating a visual Java object.

1.    Start DrJava from the Start-Programs Menu.
2.    Maximize the Interaction pane and type the following statements:
a.    Type import javax.swing.*; and press the Enter key.  What happens? ___________
b.    Type JFrame win = new JFrame("Hello") and press Enter.  What happens?_________
c.    Type win.show() and press Enter.  What happens now? _______________________
d.    Type win.setSize(50, 50) and press the Enter key.  What happens? ________________
e.    Type win.setSize(150, 150) What happens? ____________________
f.     Type win.setTitle("your name") What happens? __________________
g.    Type win.move(50, 50)  What happens? _____________________
h.    Change the dimensions of the window.  How do you do that? _____________
i.      Position the window somewhere in the middle of the screen?  How ? ____________
j.      Create another window.
k.    Set its title to Lab2.
l.      Set its size to a square.
m.   Place the window in the top right corner.


Part II.  Discovering some more

3.    In the Interactions pane type the following statements:
·         Type 56 + 23 and press the Enter key.  What happens? ________________________
·         Type 10 / 3 and press Enter.  What happens? ____________________________
·         Type 10.0 / 3.0 and press Enter.  What happens now? _______________________
·         Type 10.0 / 3 and press Enter.  What happens now? _______________________
·         Any conclusions? ____________________________________________________
·         Type System.out.println(“CS 120”) and press the Enter key.  What happens? ____________________________
·         Type System.out.println(“56 + 23”). What happens? ____________________
·         Type System.out.println(56 + 23). What happens? _______why? ___________
·         Use System.out.println() to display 10 / 3.   What happens? _____________________
·         Use System.out.println to display your first name.  How do you do that? _____________

Part III.  Testing the IDE.

4.    Type the Hello.java program shown below.
5.    Save the program in your H: drive folder CS120 subfolder Lab2 as Hello.java.
6.    Click on the Compile All icon to compile the program.  Correct errors until no errors exist.
7.    To Run the program you must call the interpreter in the Interactions panel.  Type java Hello.
8.    The output will be displayed in the Console panel as well as the Interactions.

Part IV.  Our first Simple Java Program.

1.    Click on the New button to start a new Java program. 
2.    Use the Hello program as a template but this time call it BoxName.java and save it.
3.    Write a program that prints your name centered inside a box on the screen, like this:

 






Part V.  Another  Java Program.

1.    Click on the New button to start a new Java program.  Call it Face.java.
2.    Write a program that prints a face.
3.    Look at the keyboard and select the characters that will provide you with a good face.
4.    Use comments to describe the program, write your name and the date.

Note: Show your work before you leave the lab so you can be counted as present!

Solution part 4
public
 class BoxName
{   public static void main( String [] args )
   {

      System.out.println("+-------------+" );

      System.out.println("| Car Michael |" );

      System.out.println("+-------------+");

   }
}
Solution part 5
/**
Displays a face.
*/
public class Face
{    public static void main(String[] args)
   {
     System.out.println(" /////");
     System.out.println(" | o o |");
     System.out.println(" (| ^ |)");
     System.out.println(" //| \\_/ |\\\\");
     System.out.println(" -----");
   }
}

OK

Nenhum comentário:

Postar um comentário