第一题:
import java.io.BufferedReader;
import java.io.InputStreamReader;/**
 * Created by IntelliJ IDEA.
 */
public class SumCalculator {
    private SumCalculator() {}    public static void main(String[] args) throws Exception {
        new SumCalculator().run();
    }    /**
     * business body.
     * <li> Read input </li>
     * <li> calculate sum </li>
     * <li> display sum </li>
     * @throws Exception if any error occurs
     */
    private void run() throws Exception {
        int maxNumber = readInputNumber();
        int sum = calculateSum(maxNumber);
        displayResult(sum);
    }    /**
     * Read input number from standard input. (System.in)
     * @return the number you typed
     * @throws Exception if IO error occurs or number is invalid
     */
    private int readInputNumber() throws Exception {
        System.out.println("Please type max number: ");
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String inputString = reader.readLine();
        reader.close();
        try {
            return Integer.parseInt(inputString);
        } catch(NumberFormatException ex) {
            throw new Exception("Invalid input number");
        }
    }    /**
     * Calculate the total number from 1 to maxNumber
     * @param maxNumber the max, must greater than 0
     * @return total sum
     * @throws IllegalArgumentException if max number is invalid
     */
    private int calculateSum(int maxNumber) {
        if (maxNumber<=0) {throw new IllegalArgumentException("Number must be gt 0");}
        int sum = 0;
        for ( int i=1; i<=maxNumber; i++ ) {sum+=i;}
        return sum;
    }    /**
     * Display the sum number at System.out
     * @param sum sum number
     */
    private void displayResult(int sum) {
        System.out.println("The total number is : "+sum);
    }
}

解决方案 »

  1.   

    第二题:
    只给你实现而复数,而且只有满足你需求的部分。剩下的自己完成吧。
    public final class Complex {
        private final int rp;
        private final int ip;    public Complex() {rp=ip=0;}
        public Complex(int rp, int ip) {this.rp=rp;this.ip=ip;}    public int getRealPart() {return rp;}
        public int getImaginPart() {return ip;}    public Complex addComplex(Complex c) {
            if (null==c) {throw new NullPointerException("Cannot add null complex");}
            return new Complex(rp+c.getRealPart(), ip+c.getImaginPart());
        }    private String complexString = null;
        public String toString() {
            if (complexString==null) {complexString=rp+"+"+ip+"i";}
            return complexString;
        }
    }
      

  2.   

    我来做简单的第三题public class Student{
    long id;
    String name;
    int age;
    boolean sex;
    String phone;Student( long i, String n,int a,boolean s,String p){
    this.id=i;
    this.name=n;
    this.age=a;
    this.sex=s;
    this.phone=p;
    }public int getAge(){
    return this.age;
    }public boolean getSex(){
    return this.sex;
    }public String getPhone(){
    return this.phone;
    }public long getId(){
    return this.id;
    }public String getName(){
    return this.name;
    }public String toString(){return "\nName:"+this.name+
           "\nSex:"+ ((this.sex)?"Male":"Female")+
           "\nID:"+ Long.toString(this.id)+
           "\nPhone"+this.phone;
    }
    }