Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
                                                                                                                                            Qianqian Wang
                                                                                                                                              Homework 4
                                                                                                                                               11/19/2014
1.
// Lab 1: Time2.java 
// Time2 class definition with methods tick, 
// incrementMinute and incrementHour. 
public class Time2 
{ 
    private int hour; // 0 - 23 
    private int minute; // 0 - 59 
    private int second; // 0 - 59 
    // Time2 no-argument constructor: initializes each instance variable 
    // to zero; ensures that Time2 objects start in a consistent state 
    public Time2() 
    { 
        this( 0, 0, 0 ); // invoke Time2 constructor with three arguments 
    } // end Time2 no-argument constructor 
    // Time2 constructor: hour supplied, minute and second defaulted to 0 
    public Time2( int h ) 
    { 
        this( h, 0, 0 ); // invoke Time2 constructor with three arguments 
    } // end Time2 one-argument constructor 
    // Time2 constructor: hour and minute supplied, second defaulted to 0 
    public Time2( int h, int m ) 
    { 
        this( h, m, 0 ); // invoke Time2 constructor with three arguments 
    } // end Time2 two-argument constructor 
    // Time2 constructor: hour, minute and second supplied 
    public Time2( int h, int m, int s ) 
    { 
        setTime( h, m, s ); // invoke setTime to validate time 
    } // end Time2 three-argument constructor 
   // Time2 constructor: another Time2 object supplied 
    public Time2( Time2 time ) 
    { 
      // invoke Time2 constructor with three arguments 
        this( time.getHour(), time.getMinute(), time.getSecond() ); 
    } // end Time2 constructor with Time2 argument 
   // Set a new time value using universal time. Perform 
   // validity checks on data. Set invalid values to zero. 
   /* Write header for setTime. */ 
    public void setTime(int h, int m, int s) 
    { 
      /* Write code here that declares three boolean variables which are 
         initialized to the return values of setHour, setMinute and setSecond. 
         These lines of code should also set the three member variables. */ 
        setHour( h); 
        setMinute( m); 
        setSecond( s); 
      /* Return true if all three variables are true; otherwise, return false. */ 
    } 
   // validate and set hour 
   /* Write header for the setHour method. */ 
    public void setHour(int h) 
    { 
      /* Write code here that determines whether the hour is valid. 
         If so, set the hour and return true. */ 
        if (h >= 0 && h < 24) 
            hour = h; 
        else { 
            hour = 0; 
            throw new IllegalArgumentException( "Hours must be 0~23"); 
        } 
      /* If the hour is not valid, set the hour to 0 and return false. */ 
     } 
   // validate and set minute 
   /* Write the header for the setMinute method. */ 
    public void setMinute(int m) 
    { 
      /* Write code here that determines whether the minute is valid. 
         If so, set the minute and return true. */ 
        if ( m >=0 && m < 60) 
            minute = m; 
        else{ 
            minute = 0; 
            throw new IllegalArgumentException(" Minutes must be 0~59"); 
        } 
      /* If the minute is not valid, set the minute to 0 and return false. */ 
    } 
   // validate and set second 
   /* Write the header for the setSecond method. */ 
    public void setSecond(int s) 
    { 
      /* Write code here that determines whether the second is valid. 
         If so, set the second and return true. */ 
        if (s >=0 && s < 60) 
            second = s; 
        else{ 
            second = 0; 
            throw new IllegalArgumentException("Seconds must be 0~59"); 
        } 
      /* If the second is not valid, set the second to 0 and return false. */ 
    } 
   // Get Methods 
   // get hour value 
    public int getHour() 
    { 
        return hour; 
    } // end method getHour 
   // get minute value 
    public int getMinute() 
    { 
        return minute; 
    } // end method getMinute 
    // get second value 
    public int getSecond() 
    { 
        return second; 
    } // end method getSecond 
    // Tick the time by one second 
    public void tick() 
    { 
        setSecond( second + 1 ); 
        if ( second == 0 ) 
           incrementMinute(); 
    } // end method tick 
   // Increment the minute 
   public void incrementMinute() 
   { 
      setMinute( minute + 1 ); 
      if ( minute == 0 ) 
         incrementHour(); 
    } // end method incrementMinute 
    // Increment the hour 
    public void incrementHour() 
    { 
        setHour( hour + 1 ); 
    } // end method incrementHour 
   
    // convert to String in universal-time format (HH:MM:SS) 
    public String toUniversalString() 
    { 
      return String.format( 
           "%02d:%02d:%02d", getHour(), getMinute(), getSecond() ); 
    } // 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", 
           ( ( getHour() == 0 || getHour() == 12 ) ? 12 : getHour() % 12 ), 
           getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) ); 
    } // end method toStandardString 
} // end class Time2 
// Lab 1: Time2Test.java 
// Program adds validation to Fig. 8.7 example 
import java.util.Scanner; 
public class Time2Test 
{ 
   public static void main( String args[] ) 
   { 
      Scanner input = new Scanner( System.in ); 
      
      Time2 time = new Time2(); // the Time2 object 
      int choice = getMenuChoice(); 
      
      while ( choice != 5 ) 
      { 
         switch ( choice ) 
         { 
            case 1: // set hour 
               System.out.print( "Enter Hours: " ); 
               int hours = input.nextInt(); 
               
               /* Write code here that sets the hour. If the hour is invalid, 
                  display an error message. */ 
               time.setHour(hours); 
               break;             
            case 2: // set minute 
               System.out.print( "Enter Minutes: " ); 
               int minutes = input.nextInt(); 
               
               /* Write code here that sets the minute. If the minute is invalid, 
                  display an error message. */ 
               time.setMinute(minutes); 
               break;         
            case 3: // set seconds 
               System.out.print( "Enter Seconds: " ); 
               int seconds = input.nextInt(); 
               
               /* Write code here that sets the second. If the second is invalid, 
                  display an error message. */ 
               time.setSecond(seconds); 
               break; 
            case 4: // add 1 second 
               time.tick();               
               break;               
         } // end switch 
         
         System.out.printf( "Hour: %d  Minute: %d  Second: %d\n", 
            time.getHour(), time.getMinute(), time.getSecond() ); 
         System.out.printf( "Universal time: %s   Standard time: %s\n", 
            time.toUniversalString(), time.toString() ); 
         choice = getMenuChoice(); 
      } // end while      
   } // end main 
   // prints a menu and returns a value corresponding to the menu choice 
   private static int getMenuChoice() 
   { 
      Scanner input = new Scanner( System.in ); 
      
      System.out.println( "1. Set Hour" ); 
      System.out.println( "2. Set Minute" ); 
      System.out.println( "3. Set Second" ); 
      System.out.println( "4. Add 1 second" ); 
      System.out.println( "5. Exit" ); 
      System.out.print( "Choice: " ); 
      
      return input.nextInt(); 
   } // end method getMenuChoice 
} // end class Time2Test
I have finished this task successfully!
Score 10/10
2.
// Lab 3: Complex.java 
// Definition of class Complex 
public class Complex 
{ 
    private double real; 
    private double imaginary; 
    // Initialize both parts to 0 
    /* Write header for a no-argument constructor. */ 
    public Complex() 
    { 
       /* Write code here that calls the Complex constructor that takes 2 
         arguments and initializes both parts to 0 */ 
        real = 0; 
        imaginary = 0; 
    } // end Complex no-argument constructor 
    // Initialize real part to r and imaginary part to i 
    /* Write header for constructor that takes two arguments-real part r and 
      imaginary part i. */ 
    public Complex (double r, double i) 
    { 
       /* Write line of code that sets real part to r. */ 
        real = r; 
       /* Write line of code that sets imaginary part to i. */ 
        imaginary = i; 
    } 
    // Add two Complex numbers 
    public Complex add( Complex right ) 
    { 
       /* Write code here that returns a Complex number in which the real part is 
         the sum of the real part of this Complex object and the real part of the 
         Complex object passed to the method; and the imaginary part is the sum 
         of the imaginary part of this Complex object and the imaginary part of 
         the Complex object passed to the method. */ 
        Complex temp = new Complex (this.real, this.imaginary); 
        temp.real = temp.real + right.real; 
        temp.imaginary = this.imaginary + right.imaginary; 
        return temp; 
    } 
    // Subtract two Complex numbers 
    public Complex subtract( Complex right ) 
    { 
       /* Write code here that returns a Complex number in which the real part is 
         the difference between the real part of this Complex object and the real 
         part of the Complex object passed to the method; and the imaginary part 
         is the difference between the imaginary part of this Complex object and 
         the imaginary part of the Complex object passed to the method. */ 
        Complex temp = new Complex (this.real, this.imaginary); 
        temp.real = temp.real - right.real; 
        temp.imaginary = this.imaginary - right.imaginary; 
        return temp; 
    } 
    // Return String representation of a Complex number 
    public String toString() 
    { 
        return String.format( "(%.1f, %.1f)", real, imaginary ); 
    } // end method toComplexString; 
} // end class Complex
// Exercise 8.12: ComplexTest.java 
// Test the Complex number class 
public class ComplexTest 
{ 
   public static void main( String args[] ) 
   { 
      // initialize two numbers 
      Complex a = new Complex( 9.5, 7.7 ); 
      Complex b = new Complex( 1.2, 3.1 ); 
 
      System.out.printf( "a = %s\n", a ); 
      System.out.printf( "b = %s\n", b ); 
      System.out.printf( "a + b = %s\n", a.add( b ) ); 
      System.out.printf( "a - b = %s\n", a.subtract( b ) ); 
   } // end main 
} // end class ComplexTest
I have finished this task successfully!
Score: 10/10
3.
// Lab Exercise 1: PieceWorker.java 
// PieceWorker class extends Employee. 
public class PieceWorker extends Employee 
{ 
   /* declare instance variable wage */ 
    private double wage; 
   /* declare instance variable pieces */ 
    private double pieces; 
   // five-argument constructor 
    public PieceWorker( String first, String last, String ssn, 
        double wagePerPiece, int piecesProduced ) 
    { 
      /* write code to initialize a PieceWorker */ 
        super(first,last,ssn); 
        setWage( wagePerPiece); 
    } // end five-argument PieceWorker constructor 
 
   // set wage 
   /* write a set method that validates and sets the PieceWorker's wage */ 
    public void setWage(double wagedWork) 
    { 
        wage = wagedWork; 
    } 
   // return wage 
   /* write a get method that returns the PieceWorker's wage */ 
    public double getWage() 
    { 
        return wage; 
    } 
   // set pieces produced 
   /* write a set method that validates and sets the number of pieces produced */ 
    public void setPieces(double piecesMade) 
    { 
       pieces = piecesMade; 
    } 
   // return pieces produced 
   /* write a get method that returns the number of pieces produced */ 
    public double getPieces() 
    { 
        return pieces; 
    } 
   // calculate earnings; override abstract method earnings in Employee 
    public double earnings() 
    { 
      /* write code to return the earnings for a PieceWorker */ 
        return getWage() * getPieces(); 
    } // end method earnings 
   // return String representation of PieceWorker object 
    public String toString() 
    { 
      /* write code to return a string representation of a PieceWorker */ 
        return String.format(" %s: %s\n%s: $%,.2f\n%s:%.2f","Piecework Employee", 
super.toString(),"wage is",getWage(),"number of pieces",getPieces(),earnings()); 
    } // end method toString 
} // end class PieceWorker
// Lab Exercise 1: PayrollSystemTest.java 
// Employee hierarchy test program. 
public class PayrollSystemTest 
{ 
    public static void main( String args[] ) 
    { 
      // create five-element Employee array 
        Employee employees[] = new Employee[ 5 ]; 
      // initialize array with Employees 
        employees[ 0 ] = new SalariedEmployee( 
           "John", "Smith", "111-11-1111", 800.00 ); 
        employees[ 1 ] = new HourlyEmployee( 
           "Karen", "Price", "222-22-2222", 16.75, 40 ); 
        employees[ 2 ] = new CommissionEmployee( 
           "Sue", "Jones", "333-33-3333", 10000, .06 ); 
        employees[ 3 ] = new BasePlusCommissionEmployee( 
           "Bob", "Lewis", "444-44-4444", 5000, .04, 300 ); 
       /* create a PieceWoker object and assign it to employees[ 4 ] */ 
        employees[ 4 ] = new PieceWorker( 
           "Emily", "Wang", "555-55-5555", 87.23, 234); 
        System.out.println( "Employees processed polymorphically:\n" ); 
      
       // generically process each element in array employees 
        for ( Employee currentEmployee : employees ) 
        { 
             System.out.println( currentEmployee ); // invokes toString 
             System.out.printf( 
              "earned $%,.2f\n\n", currentEmployee.earnings() ); 
        } // end for 
    } // end main 
} // end class PayrollSystemTest
I have finished this task successfully!  Score: 10/10
4.
The  relationship  that  exists  between  these  classes  is  an  “is-a”  relationship.  An
undergraduateStudent  is  a  student,  and  also,  a  graduateStudent  is  a  student.  These  classes
freshman, sophomore, junior,  senior student is undergraduateStudent,  and at the same time,
each of them is a student. MastersStudent, and DoctoralStudent is a graduatestudent. They are
also student. Inheritance makes it easy for someone to extend an idea further. This saves a lot of
time coding and a lot of time developing new methods for things that are already created. 
Score:10/10
5.
//integer that returns the number with its digits reversed 
import java.util.Scanner; 
public class Reverse 
{ 
    public static void main(String args[]) 
    { 
        Scanner input = new Scanner(System.in); 
        int num; //defines number as integer 
        int r = 0; //defines the reverse 
        System.out.printf("Enter an integer: "); //ask user for number 
        num = input.nextInt(); //input the number 
        while (num != 0) 
        { 
            r = r * 10 + num % 10; 
            num = num / 10; 
        } 
        System.out.printf("Reverse integer is %d", r); 
    } 
}
I have finished this task successfully!  
Score:10/10
6.
//determines if parameter number is a perfect number 
import java.io.*; 
import java.util.Scanner; 
public class Perfect 
{ 
    public static void main( String args[]) throws IOException 
    { 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
        System.out.print("\nEnter a number:"); 
        int n = Integer.parseInt(br.readLine()); 
        int sum = 0; 
        for(int i = 1;i < n;i++) 
        { 
            if(n%i == 0) 
            { 
                sum = sum + i; 
            } 
        } 
        if(sum == n) 
            System.out.println("\nGiven number is a perfect number"); 
        else 
            System.out.println("\nGiven number is not perfect number"); 
    } 
} 
//Java program to display perfect numbers in a given range 
//range 1~1000 
//range larger than 1000 
import java.io.*; 
class PerfectList 
{ 
    public static void main(String[] args) throws IOException 
    { 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
        System.out.print("\nEnter the range:"); 
        long range = Long.parseLong(br.readLine()); 
        System.out.println("\nThe perfect numbers between 1 and "+range+" are:"); 
        for(int i=1;i