Total de visualizações de página

sábado, 11 de dezembro de 2010

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

Nenhum comentário:

Postar um comentário