Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
 
Page 1 of 4 
 
CS 1301 – Spring 2016 
Lab 3 
 
Working with Selections – Chapter 3 
 
Use JGrasp to complete the following programs and understand what they do.  
 
Save all programs for future reference as they illustrate essential programming concepts and syntax that you 
may use in other labs and Assignments. 
 
  
//********************************************************************  
// Age.java                              Author: Lewis/Loftus  
// Demonstrates the use of if statements.  
//********************************************************************  
   import java.util.Scanner;  
   public class Age  
   {  
   //-----------------------------------------------------------------  
   // Reads the user's age and prints comments accordingly.  
   //-----------------------------------------------------------------  
      public static void main (String[] args)  
      {  
         final int MINOR = 21;  
         Scanner scan = new Scanner (System.in); 
  
         System.out.print ("Enter your age: ");  
         int age = scan.nextInt(); 
  
         System.out.println ("You entered: " + age);  
         if (age < MINOR)  
            System.out.println ("Youth is a wonderful thing. Enjoy.");  
         System.out.println ("Age is a state of mind.");  
      }  
   }  
 
 
//********************************************************************  
// Wages.java                                  Author: Lewis/Loftus  
// Demonstrates the use of an if-else statement.  
//********************************************************************  
   import java.text.NumberFormat;  
   import java.util.Scanner;  
   public class Wages  
   {  
   //-----------------------------------------------------------------  
   // Reads the number of hours worked and calculates wages.  
   //-----------------------------------------------------------------  
      public static void main (String[] args)  
      {  
         final double RATE = 8.25; // regular pay rate  
         final int STANDARD = 40; // standard hours in a work week  
         double pay = 0.0; 
 
         Scanner scan = new Scanner (System.in); 
 
         System.out.print ("Enter the number of hours worked: ");  
         int hours = scan.nextInt();        
         System.out.println (); 
           
         // Pay overtime at "time and a half"  
         if (hours > STANDARD)  
            pay = STANDARD * RATE + (hours-STANDARD) * (RATE * 1.5);  
         else  
 
Page 2 of 4 
            pay = hours * RATE; 
              
         NumberFormat fmt = NumberFormat.getCurrencyInstance();  
         System.out.println ("Gross earnings: " + fmt.format(pay));  
      }  
   }  
 
 
//********************************************************************  
// Guessing.java                               Author: Lewis/Loftus  
// Demonstrates the use of a block statement in an if-else.  
//********************************************************************  
   import java.util.*;  
   public class Guessing  
   {  
   //-----------------------------------------------------------------  
   // Plays a simple guessing game with the user.  
   //-----------------------------------------------------------------  
      public static void main (String[] args)  
      {  
         final int MAX = 100;  
         int answer, guess; 
           
         Scanner scan = new Scanner (System.in); // scanner object 
         Random generator = new Random(); // random number generator object 
          
         answer = generator.nextInt(MAX) + 1;  
          
         System.out.print ("I'm thinking of a number between 1 and "  
            + MAX + ". Guess what it is: ");  
          
        guess = scan.nextInt();  
         if (guess == answer)  
            System.out.println ("You got it! Good guessing!");  
         else  
         {  
           System.out.println ("That is not correct, sorry.");  
           System.out.println ("The number was " + answer);  
         }  
      } 
   } 
 
 
//********************************************************************  
// MinOfThree.java                            Author: Lewis/Loftus  
// Demonstrates the use of nested if statements.  
//********************************************************************  
   import java.util.Scanner;  
   public class MinOfThree  
   {  
   //-----------------------------------------------------------------  
   // Reads three integers from the user and determines the smallest  
   // value.  
   //-----------------------------------------------------------------  
      public static void main (String[] args)  
      {  
         int num1, num2, num3, min = 0;  
         
         Scanner scan = new Scanner (System.in); 
           
         System.out.println ("Enter three integers: ");  
         num1 = scan.nextInt();  
         num2 = scan.nextInt();  
         num3 = scan.nextInt();  
          
         if (num1 < num2)  
            if (num1 < num3)  
               min = num1;  
            else  
 
Page 3 of 4 
               min = num3;  
         else  
            if (num2 < num3)  
               min = num2;  
            else  
               min = num3;  
                
         System.out.println ("Minimum value: " + min);  
      }  
   }  
 
 
//********************************************************************  
// GradeReport.java                            Author: Lewis/Loftus  
// Demonstrates the use of a switch statement.  
//********************************************************************  
   import java.util.Scanner;  
   public class GradeReport  
   {  
   //-----------------------------------------------------------------  
   // Reads a grade from the user and prints comments accordingly.  
   //-----------------------------------------------------------------  
      public static void main (String[] args)  
     {  
         int grade, category;  
         Scanner scan = new Scanner (System.in);  
         System.out.print ("Enter a numeric grade (0 to 100): ");  
         grade = scan.nextInt();  
       
         category = grade / 10;  
         System.out.print ("That grade is ");  
         switch (category)  
         {  
            case 10:  
               System.out.println ("a perfect score. Well done.");  
               break;  
            case 9:  
               System.out.println ("well above average. Excellent.");  
               break;  
           case 8:  
               System.out.println ("above average. Nice job.");  
               break;  
            case 7:  
               System.out.println ("average.");  
               break;  
            case 6:  
               System.out.println ("below average. You should see the");  
               System.out.println ("instructor to clarify the material "  
                  + "presented in class.");  
               break;  
            default:  
               System.out.println ("not passing.");  
         }  
      }  
   }  
 
 
Now, use the above programs for guidance to complete the following practice programs. 
 
 
Exercise #1: Write a Java program (name it Practice_3_1) that reads from the user four grades between 0 
and 100. The program then, on separate lines, prints out the entered grades followed by the highest grade, 
lowest grade, and averages of all four grades. Make sure to properly label your output. Use escape character to 
line-up the outputs after the labels. 
 
 
 
Page 4 of 4 
Exercise #2: Write a Java program (name it Practice_3_2) that generates a random number between 0 and 
100 (representing a grade) (see program Guessing.java above or section 3.7 on how to generate random 
numbers). The program then, on separate lines, prints out the generated grade followed by the corresponding 
letter grade (A, B, C, D, F) using the standard scale below. Label your output properly and use escape 
characters to lineup the outputs after the labels. 
 
90 – 100:  A 
80 – 89:   B 
70 – 79:   C 
60 – 69:   D 
00 – 59:   F 
 
 
Exercise #3: Write a Java program (name it Practice_3_3) to implement a solution for the following problem. 
The cost of international call from Atlanta to Beijing is calculated as follows:  
 
Connection fee is $1.99;   $2.00 for first three minutes;   and $.45 for each additional minute. 
 
The program prompts the user to enter the minutes, then should output the call duration followed by the amount 
due. Use currency object format. Use proper labels and line-up your outputs using escape characters.  
 
 
Exercise #4: Write a Java program (name it Practice_3_4) that prompts the user to enter the x-coordinate 
and y-coordinate of a point (in a Cartesian plane). The program should then output a message indicating 
whether the point is the origin, is located on the x-axis, is located on the y-axis, or appears in a particular 
quadrant. For example:  
 
(0,0)  is the origin 
(4,0)  is on the x-axis 
(0,-3) is on the y-axis 
(-2,3) is in the second quadrant 
 
Document your code properly and lineup the outputs as shown above. 
 
 
Exercise #5: Write a Java program (name it Practice_3_5) that generates three single-digit numbers (see 
program Guessing.java above or section 3.7 on how to generate random numbers) and prompts the user to 
enter the sum of these three numbers. Print out the entered answer followed by the message “Correct!“  if the 
answer is correct, or the message “Incorrect! The correct answer is  ”  if the entered answer is 
incorrect. Use the following sample outputs to format your outputs. 
 
What is the sum 9  +  3 +  7? 
You entered 19  
Correct! 
 
What is the sum 9  +  3 +  7? 
You entered 15  
Incorrect! The correct answer is 19 
 
 
Submission: 
 
1.  Save all programs. 
2.  Check with your instructor for submission instructions.