Java第一次作業

elevenxl發表於2008-03-12
/*
 * @(#)Card
 *
 * Copyright 2008 School of Software, Yunnan University.
 *                                  All rights reserved
 */
package cn.edu.ynu.sei.java_homework.HW1.Part1;

/**
 * Java平時作業1,第一部分
 * @version 1.0.0.0 Feb 25, 2008
 * @author eleven
 */
public class Card {

    public void Card(String name, String address, String email, String phone) {
        System.out.println("--------------------------------");
        System.out.println("Name: " + name);
        System.out.println("Address: " + address);
        System.out.println("Email: " + email);
        System.out.println("Phone: " + phone);
        System.out.println("--------------------------------");
    }

    public static void main(String[] args) {
        Card card = new Card();
        card.Card("Eleven", "Software School, Yunnan University", "eleven026@gmail.com", "12888546307");
    }
}




/*
 * @(#)ComputeInterest
 *
 * Copyright 2008 School of Software, Yunnan University.
 *                                  All rights reserved
 */
package cn.edu.ynu.sei.java_homework.HW1.Part2;

/**
 * Java平時作業,第一次第2部分,計算利息
 * @version 1.0.0.0 Feb 25, 2008
 * @author eleven
 */
public class ComputeInterest {

    private double interestRate = 0.05;
    private static double balance;

    public ComputeInterest() {
        System.out.println("-------------------------------------------------------------");
        System.out.println("The balance is " + balance);
        System.out.println("---");
        double newbalance_year1 = balance * (1 + interestRate);
        System.out.println("The balance at the end of the first year is " + newbalance_year1);
        double newbalance_year2 = newbalance_year1 * (1 + interestRate);
        System.out.println("The balance at the end of the second year is " + newbalance_year2);
        double newbalance_year3 = newbalance_year2 * (1 + interestRate);
        System.out.println("The balance at the end of the third year is " + newbalance_year3);
        double newbalance_year4 = newbalance_year3 * (1 + interestRate);
        System.out.println("The balance at the end of the forth year is " + newbalance_year4);
        double newbalance_year5 = newbalance_year4 * (1 + interestRate);
        System.out.println("The balance at the end of the fifth year is " + newbalance_year5);
        System.out.println("-------------------------------------------------------------");
    }

    public static void main(String[] args) {
        balance = 10000;
        ComputeInterest computeInterest = new ComputeInterest();
    }
}



/*
 * @(#)PrimeGenerator
 *
 * Copyright 2008 School of Software, Yunnan University.
 *                                  All rights reserved
 */
package cn.edu.ynu.sei.java_homework.HW1.Part3;

/**
 * Java平時作業,第一次第3部分,產生素數
 * @version 1.0.0.0 Mar 11, 2008
 * @author eleven
 */
public class PrimeGenerator {

    /**
     * 根據給出的起始數字,判斷併產生素數
     */
    public int IsPrime(int number) {
        int returnValue = 0;
        if (number == 2) {
            returnValue = 1;
        } else {
            for (int i = 2; i < number; i++) {
                if (number % i != 0) {
                    returnValue = 1;
                } else {
                    returnValue = 0;
                    break;
                }
            }
        }
        return returnValue;
    }
}


/*
 * @(#)GetPrime
 *
 * Copyright 2008 School of Software, Yunnan University.
 *                                  All rights reserved
 */
package cn.edu.ynu.sei.java_homework.HW1.Part3;

import java.util.ArrayList;
import java.util.List;

/**
 * Java平時作業,第一次第3部分,得到素數
 * @version 1.0.0.0 Mar 12, 2008
 * @author eleven
 */
public class GetPrime {

    /**
     * 根據給出的起始數字,呼叫PrimeGenerator類中的IsPrime()方法對起始數字之間的所有數字進行判斷,
     * 並將判斷的素數儲存到ArrayList中,供Prime類呼叫。
     */
    public List GetPrime(int startNumber, int endNumber) {
        List primeArrayList = new ArrayList();
        PrimeGenerator primeGenerator = new PrimeGenerator();
        for (int i = startNumber; i <= endNumber; i++) {
            if (primeGenerator.IsPrime(i) == 1) {
                primeArrayList.add(i);
            }
        }
        System.out.println("The Primes you want to get  between " + startNumber + " and " + endNumber + " are");
        return primeArrayList;
    }
}

/*
 * @(#)Prime
 *
 * Copyright 2008 School of Software, Yunnan University.
 *                                  All rights reserved
 */
package cn.edu.ynu.sei.java_homework.HW1.Part3;

import java.util.List;

/**
 * Java平時作業,第一次第3部分,main函式
 * @version 1.0.0.0 Mar 12, 2008
 * @author eleven
 */
public class Prime {

    public static void main(String[] args) {
        GetPrime getPrime = new GetPrime();
        List prime = getPrime.GetPrime(1, 100);
        for (Object primeNumber : prime) {
            System.out.println(primeNumber);
        }
    }
}


 

相關文章