Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
1 
 
Lab 7 
Decision Control Structures 
The following exercises are to be completed during lab class.  If you do not 
have time to finish during lab, they must be completed before the 
beginning of the following lab session.   
 
 
Set-Up 
 Create a new project in your Eclipse workspace named:  Lab07 
 In the src folder, create a package named:  edu.ilstu 
 Import into this new package all of the following .java files from           
T:/it168/Labs/Lab07 
o DivideTwo.java 
o PersonTester.java 
 
Part 1 – if/else Statement 
Open the file DivideTwo.java.  The Java source code is shown below: 
 
package edu.ilstu; 
import java.util.Scanner; 
 
public class DivideTwo  
{ 
 public static void main(String[] args)  
 {  
  Scanner keyboard = new Scanner(System.in); 
  int numerator = 0; 
  int denominator = 0; 
  double quotient = 0; 
   
  System.out.println("This program divides two numbers."); 
  System.out.print("Enter the numerator:  "); 
  numerator = keyboard.nextInt(); 
   
  System.out.print("Enter the denominator:  "); 
  denominator = keyboard.nextInt(); 
  quotient = (double) numerator / denominator; 
   
  System.out.println(numerator + "/" + denominator  
+ " = " + quotient); 
   
  keyboard.close();  
} 
} 
 
2 
 
Compile and run DivideTwo.java, and observe the output.  The program prompts 
the user for two integers that represent the numerator and the denominator of a 
fraction.  The Scanner object named keyboard is used to read the integers 
provided by the user. 
 
Modify the program to include an if/else statement to check for division by zero.  
If the denominator is not equal to zero, display the result of the division, otherwise 
display a message to the user that division by zero is not allowed. 
 
 
Part 2 – if/else Statement 
 
Write the Java source code to implement your pseudocode solution written for pre-
lab.  Just create one class with a main method and name it NumberCategory.java 
 
Write a program to determine if a number entered by the user is positive, negative, or 
zero.  Display a prompt to let the user know what types of numbers should be entered.  
Display the number with the classification (positive, negative, zero) to the user. 
 
 
 
  
3 
 
Part 3 – if/else Statement 
Write the code for the Person class shown in this class diagram then test it with the provided file:  
PersonTester.java 
Person 
- title:String 
- name:String 
- gender:String 
- maritalStatus:String 
+ Person(String name, String gender, String maritalStatus) 
+ determineTitle():void 
+ isValidGender():boolean 
+ isValidMaritalStatus:boolean 
+ getTitle():String 
+ getName():String 
+ setName(String name):void 
+ getGender():Sting 
+ setGender(String gender):void 
+ getMaritalStatus():String 
+ setMaritalStatus(String maritalStatus):void 
 
Method Descriptions 
 determineTitle 
o Uses the values for gender and maritalStatus to assign either Mr., 
Mrs., or Miss to the title variable. 
 isValidGender 
o For gender to be valid, it can only hold “F” or “M”.  This method 
compares the value stored in gender to check whether it is valid.  If it 
is either “F” or “M”, then it is valid and should return true.  Otherwise, 
it should return false.  
 isValidMaritalStatus 
o For maritalStatus to be valid, it can only hold “S” or “M” for Single or 
Married.  This method compares the value stored in marital status to 
check whether it is valid.  If it is either “S” or “M”, then it is valid and 
should return true.  Otherwise, it should return false.  
 setGender 
o Make sure the value is uppercase before storing in the instance 
variable. 
 setMaritalStatus 
o Make sure the value is uppercase before storing in the instance 
variable. 
 Note that there is no setter for title since that is set by calling the 
determineTitle method. 
4 
 
 
To Be Submitted 
The following files should be zipped together into a file called Lab07.zip and 
submitted to ReggieNet by the beginning of your next lab.   
 
 DivideTwo.java 
 NumberCategory.java 
 Person.java