Total de visualizações de página

sábado, 11 de dezembro de 2010

Data and Expressions

Laboratory #4: Data and Expressions - Review

Laboratory #4: Data and Expressions - Review

Part I. Computing Averages.

Write a program that reads three grades and prints the average.
a.      Read 3 values from the user.
b.      Calculate the average and display it.

Part II.  Converting Fahrenheit to Centigrade’s.

Write a program that converts degrees from Fahrenheit to Centigrade’s.
c.      Read the Fahrenheit value from the user.
d.      Convert the Fahrenheit degrees to Centigrade’s and display it.
e.      Use Google to find the conversion formula.

Part III.  From seconds to hours, minutes, seconds.

Write an application that reads values representing time duration in seconds, then print the equivalent in hours, minutes and seconds.  For example, 9999 seconds is equivalent to 2 hours, 46 minutes and 39 seconds.


// Nilton Soares - September, 27th 2010 - CS 120
// Program to Display Grades Results
import java.util.Scanner;

public class Gradesresult
{
  public static void main(String args[])
  {
        
       double grade=0.0;
       Scanner scan = new Scanner(System.in);   
       System.out.print("Enter the Grade (0-100): ");
       String grade=scan.next();
            
      
       while (grade<0.0 || grade>100.0)
       {
       System.out.print("Enter a valid Grade (0 to 100): ");
       grade=scan.nextDouble();
       }
       if (grade>=90.0 && grade<=100.0)
             System.out.print("Excelent!! Your grade is "+grade+"! You got A!");
       if (grade>=80.0&&grade<=89.0)
             System.out.print("Good!! Your grade is "+grade+"! You got B!");
       if (grade>=70.0&&grade<=79.0)
             System.out.print("Your grade is "+grade+"! You got C!");
       if (grade>=60.0&&grade<=69.0)
             System.out.print("Your grade is "+grade+"! You got D!");
       if (grade>=0&&grade<=59.0)
             System.out.print("Your grade is "+grade+"! You got F!");
      
  }
 
}

// Nilton Soares - September, 27th 2010 - CS 120
// Massbay - Professor Susanne Steiger-Escobar
// Program to Display Fahrenheit to Centigrade’s
import java.util.Scanner;
public class Fahrenheit_centigrade
{
  public static void main(String args[])
  {
        
       int temp_f,temp_c;
             
       Scanner scan = new Scanner(System.in);   
       System.out.print("Enter the Temperature in F: ");
       temp_f=scan.nextInt();
       temp_c=((temp_f-32)*5/9);  //Deduct 32, then multiply by 5, then divide by 9
       System.out.println("The Temperature in C is : "+temp_c);  
      
  }
 
}
// Nilton Soares - November, 19th 2010 - CS 120
// Massbay - Professor Susanne Steiger-Escobar
// Class to keep track of time by keeping h,m,s

public class Time1
{
  /**
   Initializes the Class Time Variables
   hour as integer
   minute as integer
   second as integer
   */
  private int hour;   // 0 - 23  
  private int minute; // 0 - 59  
  private int second; // 0 - 59  10   
 
  /**
   * Validate hour 0-23
   * Validate Minute 0-59
   * validade Second 0-59
   */
 
  public void setTime( int h, int m, int s ) 
  {
    hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
    minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
    second = ( ( s >= 0 && s < 60 ) ? s : 0 );
  }
  // convert to String in universal-time format (HH:MM:SS)
  public String timeOfDay(int h) 
  {
    String timeday="tessrsrsdte";
    //timeday= ((h == 5) ? "Dawn":"");
    //timeday= ((h >= 6 && h < 11) ? "Morning":"");
    //timeday= ((h >= 12 && h < 17) ? "Afternoon":"");
    //timeday= ((h >= 18 && h < 19) ? "Evening":"");
    //timeday= ((h > 19 && h < 5) ? "Night":"");
    return "dsdsds";
  }
    //Provide a method timeOfDay() that returns the string “dawn”, “morning”, “afternoon”, “evening” or “night” depending on the time of the day.
    //  ·        Provide a method isMorning() that returns true if it is morning.
     
     
     
      public String toUniversalString() 
    {
      return String.format( "%02d:%02d:%02d", hour, minute, second ); 
    } // end method toUniversalString 
    // convert to String in standard-time format (H:MM:SS AM or PM)
    public String toString() 
    {
      return String.format( "%d:%02d:%02d %s",             
                           ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ), 
                           minute, second, ( hour < 12 ? "AM" : "PM" ) );    
    } // end method toString 
  } // end class Time1

How to Evaluate Expressions in Java

Part 1. How will Java evaluate the following expressions

1.
What output is produced by System.out.println(“50 plus 25 is “ + 50 + 25 ); ________________________________
2.
What output is produced by System.out.println(“50 plus 25 is “ +”50” + “25” ); ________________________________
3.
What output is produced by System.out.println(“50 plus 25 is “ +( 50 + 25) ); ________________________________
4.
19 % 3 ?  _______________
5.
13 / 4 ? ____________________
6.
diameter = 5;
diameter = diameter * 4;
what is the value of the variable diameter now? __________________
7.
weight = 100;
weight -= 17:
what is the value of the variable weight now? ___________________
8.
size = 18;
size = size + 12;
size = size * 2;
size = size / 4;
what is the value of the variable size now? ___________________
9.
depth = 2.4;
depth = 20 – depth * 4;
depth = depth / 5;
what is the value of the variable depth now? ___________________
10.
Is we assume that we have:
double x = 2.5;
double y = -1.5;
int m = 18;
int n = 4;
String s = “Hello”;
String t = “World”;

What are the values for the following expressions? 
 x + n * y – (x + n) * y        _____________
m / n + m % n        _____________
5 * x – n / 5        _____________
s + t         _____________
s + n         _____________
11.Recalling what you've learned about integers and floating-point values, what value is assigned to x by each of the following?
int x = 6 / 3;
1)_____________
int x = 7 / 3;
2)_____________
double x = 7 / 3;
3)_____________
int x = 7 % 3;
4)_____________
int x = 6 % 3;
5)_____________
int x = 999 / 1000;
6)_____________
double x = 999.0 / 1000.0;
7)_____________
int x = (int)(999 / 1000.00);
8)_____________

Part 2. Using Numbers and Constants

1.
Suppose you have 5 1/2 gallons of milk and want to store them in milk jars that can hold up to 0.75 gallons each. How many completely filled jars you will have. What is wrong with the code? Why? How can you fix it?
public class MilkJarCalculator
{ 

   public static void main(String args[]) 
   {       

     double milk = 5.5;                // gallons      
     double jarCapacity = 0.75;        // gallons       
     int completelyFilledJars = milk / jarCapacity;      
     System.out.println(“the number of jars is “ + completelyFilledJars);   
    } } 
2.
You want to know how many feet is 3.5 yards, and how many inches is 3.5 yards.   Write a program that will provide a conversion.  Remember that 1 yard is 3 feet and 1 feet is 12 inches.
public class DistanceConverter {
    public static void main(String args[])  {
       ? yards = 3.5;

    }
}

The problem with the program above is that using "magic numbers" will make it hard to maintain and debug.  Write an application and use constants to solve this problem.  Make the output nice and readable.
3.
Provide an application that converts miles to kilometers.  (One mile equals 1.60935 kilometers.  Use the Scanner class to read the miles value from the user as a floating point value.
4.
Write an application that reads values representing a time duration in hours, minutes and seconds, and then prints the equivalent total number of seconds.  For example, 1 hour, 28 minutes and 42 seconds is equivalent to 5322 seconds.

Part 3. Prompting the User

1.
Write an application that prompts the user for an integer representing the length of a square's side.  Read the length and print the square's perimeter and area. 
Make sure you show me your work before you leave so that you can be counted as present.
Item Solution

Part 1. How will Java evaluate the following expressions

1.
What output is produced by System.out.println(“50 plus 25 is “ + 50 + 25 ); ____50 plus 25 is 5025________
2.
What output is produced by System.out.println(“50 plus 25 is “ +”50” + “25” ); ____50 plus 25 is 5025_______
3.
What output is produced by System.out.println(“50 plus 25 is “ +( 50 + 25) ); ____50 plus 25 is 75___________
4.
19 % 3 ?  ____1______
5.
13 / 4 ? _____3__________
6.
diameter = 5;
diameter = diameter * 4;
what is the value of the variable diameter now? _____20______
7.
weight = 100;
weight -= 17:
what is the value of the variable weight now? ______83______
8.
size = 18;
size = size + 12;
size = size * 2;
size = size / 4;
what is the value of the variable size now? ______15____
9.
depth = 2.4;
depth = 20 – depth * 4;
depth = depth / 5;
what is the value of the variable depth now? ___________________
10.
Is we assume that we have:
double x = 2.5;
double y = -1.5;
int m = 18;
int n = 4;
String s = “Hello”;
String t = “World”;
What are the values for the following expressions? 
 x + n * y – (x + n) * y        ____6.25
m / n + m % n        ____6______
5 * x – n / 5        ____12.5_____
s + t         _HelloWorld__
s + n         ___Hello4__

11.
int x = 6 / 3;
1) 2
int x = 7 / 3;
2) 2
double x = 7 / 3;
3) 2.0
int x = 7 % 3;
4) 1
int x = 6 % 3;
5) 0
int x = 999 / 1000;
6) 0
double x = 999.0 / 1000.0;
7) 0.999
int x = (int)(999 / 1000.00);
8) 0






Part 2. Using Numbers and Constants

1.
Suppose you have 5 1/2 gallons of milk and want to store them in milk jars that can hold up to 0.75 gallons each. How many completely filled jars you will have. What is wrong with the code? Why? How can you fix it?
public class MilkJarCalculator
{
   
   public static void main(String args[])
    {
      
     double milk = 5.5;                // gallons
      
     double jarCapacity = 0.75;        // gallons
      
     double completelyFilledJars = milk / jarCapacity; 
      
     System.out.println(“the number of jars is “ + (int) completelyFilledJars);
  
   }
} 


2.
You want to know how many feet is 3.5 yards, and how many inches is 3.5 yards.   Write a program that will provide a conversion.  Remember that 1 yard is 3 feet and 1 feet is 12 inches.
public class DistanceConverter {
    public static void main(String args[])  {
       double yards = 3.5;
       final int FEET_TO_YARDS = 3;
       final int INCHES_TO_FEET = 12;
     System.out.println("the number of feet in " + yards + " is " + (yards * FEET_TO_YARDS));
     System.out.println("the number of inches in " + yards + " is " + yards * FEET_TO_YARDS * INCHES_TO_FEET);
    }
}
The problem with the program above is that using "magic numbers" will make it hard to maintain and debug.  Write an application and use constants to solve this problem.  Make the output nice and readable.
 
Provide an application that converts miles to kilometers.  (One mile equals 1.60935 kilometers.  Use the Scanner class to read the miles value from the user as a floating point value.
import java.util.Scanner;
public class MilesToKilometers
{
  
//-----------------------------------------------------------------   //  Converts miles into kilometers. The value for miles is read   //  from the user.   //-----------------------------------------------------------------   public static void main (String[] args)
   {
     
final double MILES_PER_KILOMETER = 1.60935;

     
double miles, kilometers;

       Scanner scan =
new Scanner(System.in);

      System.out.print (
"Enter the distance in miles: ");
      miles = scan.nextDouble();

      kilometers = MILES_PER_KILOMETER * miles;

      System.out.println (
"That distance in kilometers is: " +
                          kilometers);
   }
}
4.
Write an application that reads values representing a time duration in hours, minutes and seconds, and then prints the equivalent total number of seconds.  For example, 1 hour, 28 minutes and 42 seconds is equivalent to 5322 seconds.
import java.util.Scanner;
public class SquareCalculations
{
  
//--------------------------------------------------------------   //  Computes a squares perimeter and area given the length of   //  one side.   //---------------------------------------------------------------   public static void main (String[] args)
   {
     
int side, perimeter, area;

       Scanner scan =
new Scanner(System.in);

      System.out.print (
"Enter the length of a square's side: ");
      side = scan.nextInt();

      perimeter = side * 4;
      area = side * side;

      System.out.println (
"Perimeter: " + perimeter);
      System.out.println (
"Area: " + area);
   }
}

OK

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