Java语言程序设计与数据结构(基础篇)课后练习题 第十章(一)

10.1

public class dishizhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Time time1 = new Time();
	Time time2 = new Time(555550000);
	System.out.printf("%dh/%dm/%ds\n", time1.getHour(), time1.getMinute(), time1.getSecond());
	System.out.printf("%dh/%dm/%ds\n", time2.getHour(), time2.getMinute(), time2.getSecond());
}

}

class Time {

private int hour;
private int minute;
private int second;
public Time() {
	long t = System.currentTimeMillis();
	long seconds = t / 1000;
	second = (int) seconds % 60;
	seconds /= 60;
	minute = (int) seconds % 60;
	seconds /= 60;
	hour = (int) seconds % 24;
}

public Time(long t) {
	long seconds = t / 1000;
	second = (int) seconds % 60;
	seconds /= 60;
	minute = (int) seconds % 60;
	seconds /= 60;
	hour = (int) seconds % 24;
}

int getHour() {
	return hour;
}

int getMinute() {
	return minute;
}

int getSecond() {
	return second;
}

public void setTime(long elapseTime) {
	long seconds = elapseTime / 1000;
	second = (int) seconds % 60;
	seconds /= 60;
	minute = (int) seconds % 60;
	seconds /= 60;
	hour = (int) seconds % 24;
}

}

10.2

前面课文有提及,添加一个方法就行。

public class BMI {

private String name;
private int age;
private double weight;
private double height;
public static final double KILOGRAMS_PER_POUND = 0.45359237;
public static final double METERS_PER_INCH = 0.0254;
public BMI(String name,int age,double weight,double height){
    this.name = name;
    this.weight = weight;
    this.height = height;
    this.age = age;
}

public BMI(String name,double weight,double height){
    this(name,20,weight,height);
}

public double getBMI(){
    double bmi = weight * KILOGRAMS_PER_POUND / ((height * METERS_PER_INCH) * (height * METERS_PER_INCH));
    return Math.round(bmi * 100) / 100.0;
}

public String getStatus(){
    double bmi = getBMI();
    if (bmi < 18.5)
        return "Underweight";
    else if (bmi < 25)
        return "Normal";
    else if (bmi < 30)
        return "Overweight";
    else
        return "Obese";
}

public String getName(){
    return name;
}

public int getAge(){
    return age;
}

public double getWeight(){
    return weight;
}

public double getHeight() {
    return height;
}

public BMI(String name,int age,double weight,double feet,double inches){
    this.name = name;
    this.age = age;
    this.weight = weight;
    this.height = feet * 12 + inches;
}

}

10.3

import java.util.Scanner;

public class dishizhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input = new Scanner(System.in);
	System.out.print("Enter n1: ");
	MyInteger n1 = new MyInteger(input.nextInt());
	System.out.println("Is n1 a even?" + '\t' + n1.isEven());
	System.out.println("Is n1 a prime?" + '\t' + n1.isPrime());
	System.out.println("Is n1 a prime2?" + '\t' + MyInteger.isPrime(n1));
	System.out.println("Is n1 a odd?" + '\t' + n1.isOdd());
	System.out.println("Is n1 a odd2?" + '\t' + MyInteger.isOdd(n1));
	System.out.print("Enter n2: ");
	MyInteger n2 = new MyInteger(input.nextInt());
	System.out.println("Are n1 and n2 equal?" + '\t' + n1.equals(n2));
	System.out.println("Are n1 and 5 equal?" + '\t' + n1.equals(5));
}

}

class MyInteger {

private int value;
public MyInteger(int value) {
	this.value = value;
}

public int getValue() {
	return value;
}

public static boolean isEven(int value) {
	if (value % 2 == 0)
		return true;
	else
		return false;
}

public boolean isEven() {
	return isEven(value);
}

public boolean isOdd() {
	return isOdd(value);
}

public boolean isOdd(int value) {
	if (value % 2 != 0)
		return true;
	else
		return false;
}

public boolean isPrime() {
	return isPrime(value);
}

public static boolean isPrime(int value) {
	for (int i = 2; i < value - 1; i++)
		if (value % i == 0)
			return false;
	return true;
}

public static boolean isEven(MyInteger m) {
	if (m.getValue() % 2 == 0)
		return true;
	else
		return false;
}

public static boolean isOdd(MyInteger m) {
	if (m.getValue() % 2 != 0)
		return true;
	else
		return false;
}

public static boolean isPrime(MyInteger m) {
	for (int i = 2; i < m.getValue() - 1; i++) {
		if (m.getValue() % 2 == 0)
			return false;
	}
	return true;
}

public boolean equals(int value) {
	if (this.value == value)
		return true;
	else
		return false;
}

public boolean equals(MyInteger m) {
	if (this.value == m.getValue())
		return true;
	else
		return false;
}

}

10.4

public class dishizhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	MyPoint p1 = new MyPoint(0,0);
	MyPoint p2 = new MyPoint(10,30.5);
	System.out.println("The distance is "+p1.distance(p2));
}

}

class MyPoint{

	private double x;
	private double y;
	MyPoint(){
		this.x = 0.0;
		this.y = 0.0;
	}
	public MyPoint(double x,double y){
		this.x = x;
		this.y = y;
	}
	public double distance(double x,double y){
		double s;
		s = Math.sqrt((this.x - x)*(this.x - x)+(this.y - y)*(this.y - y));
		return s;
	}
	public double distance(MyPoint mypoint){
		double s;
		s = Math.sqrt((this.x - mypoint.getX()) * (this.x - mypoint.getX())+(this.y - mypoint.getY()) * (this.y - mypoint.getY()));
		return s;
	}
	public double getX(){
		return x;
	}
	public double getY(){
		return y;
	}

}

10.5

import java.util.Arrays;
import java.util.Scanner;

public class dishizhang {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.print("Please input a integer :");
    Scanner input = new Scanner(System.in);
    int integer=input.nextInt();
    StackOfIntegers stackOfIntegers = new StackOfIntegers(integer);
    int[] i=stackOfIntegers.Prime();
    System.out.print("All the prime factors of the "+integer+ " are :");
    System.out.print(Arrays.toString(i));
}

}

class StackOfIntegers{

private int integer;
public StackOfIntegers(int integer) {
	this.integer = integer;
}
private static int k = 0;
private static int l = 0;

public int[] GetPrime() {
    int[] getPrime = new int[this.integer];
    boolean isPrime = true;
    for (int i = 2; i < this.integer; i++) {
        for (int j = 2; j < i; j++) {
            if (i % j == 0) {
                isPrime = false;
                break;
            }
        }
        if (isPrime) {
            getPrime[k] = i;
            k++;
        } else isPrime = true;
    }
    return getPrime;
}

public int[] Prime() {
    int[] get = GetPrime();
    int[] prime1 = new int[k];
    boolean judge=false;
    while (this.integer != 0) {
        for (int a = k - 1; a >= 0; a--) {
            if ((int) (this.integer / get[a]) == (double) this.integer / get[a]) {
                prime1[l] = get[a];
                this.integer /= get[a];
                l++;
            }
            if (integer == get[a] || integer == 1) {
                prime1[l] = get[a];
                judge=true;
                break;
            }
        }
        if (judge)
            break;
    }
    int[] prime=new int[l+1];
    for (int i=0;i<l+1;i++){
        prime[i]=prime1[i];
    }
    Arrays.sort(prime);
    return prime;
}

}

10.6

import java.util.Arrays;
import java.util.Scanner;

public class dishizhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	System.out.print("Please input a integer :");
    Scanner input = new Scanner(System.in);
    int integer=input.nextInt();
    StackOfIntegers stackOfIntegers = new StackOfIntegers(integer);
    int[] i = stackOfIntegers.GetPrime();
    System.out.print("All primes less than "+integer+ " are: ");
    for(int j=0;j<i.length;j++){
    	if(i[j]!=0)
    		System.out.print(i[j]+" ");
    }
   }

}

class StackOfIntegers{

 ```
    private int integer;
    public StackOfIntegers(int integer) {
        this.integer = integer;
    }
    private static int k = 0;
    public int[] GetPrime() {
        int[] getPrime = new int[this.integer];
        boolean isPrime = true;
        for (int i = 2; i < this.integer; i++) {
            for (int h = 2; h < i; h++) {
                if (i % h == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                getPrime[k] = i;
                k++;
            } else isPrime = true;
        }
        return reverse(getPrime);
}
public int[] reverse(int[] prime){
	int tmp;
	for(int i=0; i<prime.length/2; i++) {
        tmp = prime[i];
        prime[i] = prime[prime.length-1-i];
        prime[prime.length-1-i] = tmp;
    }
	return prime;
}
 ```

}

10.7

import java.util.Date;
import java.util.Scanner;

public class dishizhang {

public static void main(String[] args) {
    Account[] accounts = new Account[10];
    for (int i = 0; i < 10; i++)
        accounts[i] = new Account(1, 100);

    System.out.print("Enter an id:");
    Scanner input = new Scanner(System.in);
    int id = input.nextInt();
    while (id < 0 || id > 9) {
        System.out.print("The if is nonExistent,please input again:");
        id = input.nextInt();
    }
    mainMenu();
    int choice = input.nextInt();
    boolean judge = choice == 1 || choice == 2 || choice == 3;
    while (judge) {
        switch (choice) {
            case 1:
                System.out.println("The balance is "+accounts[id].getBalance());
                break;
            case 2:
                System.out.print("Enter an amount to withdraw: ");
                double withdraw = input.nextDouble();
                accounts[id].withdraw(withdraw);
                break;
            case 3:
                System.out.print("Enter an amount to deposit:");
                double deposit=input.nextDouble();
                accounts[id].deposit(deposit);
                break;
        }
        mainMenu();
        choice=input.nextInt();
        judge = choice == 1 || choice == 2 || choice == 3;
    }
    dishizhang.main(args);
}

public static void mainMenu(){
    System.out.println("Main menu");
    System.out.println("1: check balance ");
    System.out.println("2: withdraw ");
    System.out.println("3: deposit ");
    System.out.println("4: exit ");
    System.out.print("Enter a choice: ");
}

}

class Account {

public Account() {
    dateCreated = new Date();
}

public Account(int id,double balance){
    this.id = id;
    this.balance = balance;
    dateCreated = new Date();
}

private int id;
private double balance;
private double annualInterestRate;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public double getBalance() {
    return balance;
}

public void setBalance(double balance) {
    this.balance = balance;
}

public double getAnnualInterestRate() {
    return annualInterestRate;
}

public void setAnnualInterestRate(double annualInterestRate) {
    this.annualInterestRate = annualInterestRate;
}

private Date dateCreated=new Date();

public void Account() {
}

public double getMonthlyInterest() {
    return balance*(annualInterestRate/100/12);
}

public void withdraw(double reduce) {
    balance-=reduce;
}

public void deposit(double increase) {
    balance+=increase;
}

public Date getDateCreated() {
    return dateCreated;
}

}

  • 13
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xupengboo

你的鼓励将是我创作最大的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值