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

9.1

package demo;

public class dijiuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Rectangle r1 = new Rectangle(4, 40);
	Rectangle r2 = new Rectangle(3.5, 35.9);
	System.out.println("r1 " + r1.getWidth() + " " + r1.getHeight() + " " + r1.getArea() + " " + r1.getPerimeter());
	System.out.println("r2 " + r2.getWidth() + " " + r2.getHeight() + " " + r2.getArea() + " " + r2.getPerimeter());
}

}
class Rectangle {

private double width;
private double height;
public Rectangle() {
	this(1, 1);
}

public Rectangle(double w, double h) {
	this.width = w;
	this.height = h;
}

public double getArea() {
	return width * height;
}

public double getPerimeter() {
	return 2 * (width * height);
}

public double getWidth() {
	return width;
}

public double getHeight() {
	return height;
}

}

9.2

package demo;

public class dijiuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Stock stock = new Stock("ORCL","Oracle Corporation");
	stock.setPreviousClosingPrice(34.5);
	stock.setCurrentPrice(34.35);
	System.out.println("The change percent is "+stock.getChangePercent()+"%");
}

}
class Stock {

private String symbol;
private String name;
private double previousClosingPrice;
private double currentPrice;
Stock(String symbol,String name){
	this.symbol = symbol;
	this.name = name;
}
public void setPreviousClosingPrice(double previousClosingPrice){
	this.previousClosingPrice = previousClosingPrice;
}

public void setCurrentPrice(double price){
	this.currentPrice = price;
}

public double getChangePercent(){
	return (currentPrice-previousClosingPrice)/previousClosingPrice*100;

}

}

9.3

package demo;

public class dijiuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	long seconds = 10000;
	for(int i=0;i<8;i++){
		java.util.Date date = new java.util.Date(seconds);
		System.out.println(date.toString());
		seconds *= 10;
	}
}

}

9.4

package demo;

public class dijiuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	java.util.Random random = new java.util.Random(1000);
	for(int i=0;i<50;i++)
		System.out.print(random.nextInt(100)+" ");
}

}

9.5

package demo;

import java.util.GregorianCalendar;

public class dijiuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	GregorianCalendar g = new GregorianCalendar();
	System.out.println(g.get(GregorianCalendar.YEAR)+"/"+g.get(GregorianCalendar.MONTH)+"/"+g.get(GregorianCalendar.DAY_OF_MONTH));
	g.setTimeInMillis(1234567898765L);
	System.out.println(g.get(GregorianCalendar.YEAR)+"/"+g.get(GregorianCalendar.MONTH)+"/"+g.get(GregorianCalendar.DAY_OF_MONTH));
	
}

}

9.6

package demo;

public class dijiuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	StopWatch time = new StopWatch();
	int[] array = new int[100000];
    for (int i = 0; i < 100000; i++) {
        array[i] = (int) (Math.random() * 1000000);
    }
	time.start();
	for (int i = 0; i < array.length - 1; i++) {
		int currentMin = array[i];
		int currentMinIndex = i;
		for (int j = i + 1; j < array.length; j++) {
			if (currentMin > array[j]) {
				currentMin = array[j];
				currentMinIndex = j;
			}
		}

		if (currentMinIndex != i) {
			array[currentMinIndex] = array[i];
			array[i] = currentMin;
		}
	}
	time.stop();
	System.out.println("timeMill: "+time.getElapsedTime()); //这里生成的是毫秒。
}

}
class StopWatch{

	private long startTime;
	private long endTime;
	StopWatch(){
		this.startTime = System.currentTimeMillis();
	}
	public void start(){
		this.startTime = System.currentTimeMillis();
	}
	public void stop(){
		this.endTime = System.currentTimeMillis();
	}
	public long getElapsedTime(){
		return this.endTime-this.startTime;
	}

}

9.7

package demo;

import java.util.Date;

public class dijiuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Account s = new Account(1122,2000);
    s.setAnnualInterestRate(4.5/100);
    s.withDraw(2500);
    s.deposit(3000);
    System.out.println("Balance: "+s.getBalance());
    System.out.println("Monthly Interest: "+s.getMonthlyInterest());
    System.out.println("Register Date: "+s.getDateCreated().toString());
}

}
class Account{

	private int id;
    private double balance;
    private double annualInterestRate;
    private Date dateCreated;
    public Account(){
        id=0;
        balance=0;
        annualInterestRate=0;
        dateCreated=new Date();
    }
    public Account(int di,double b){
        id=di;
        balance=b;
        annualInterestRate=0;
        dateCreated=new Date();
    }
    public int getId(){
        return id;
    }
    public void setId(int j){
        id=j;
    }
    public double getBalance(){
        return balance;
    }
    public void setBalance(double j){
        balance=j;
    }
    public double getAnnualInterestRate(){
        return annualInterestRate;
    }
    public void setAnnualInterestRate(double j){
        annualInterestRate=j;
    }
    public Date getDateCreated(){
        return dateCreated;
    }
    public double getMonthlyInterestRate(){
        return annualInterestRate/12;
    }
    public double getMonthlyInterest(){
        return annualInterestRate/12*balance;
    }
    public void withDraw(double m){
        balance -= m;
    }
    public void deposit(double m){
        balance += m;
    }

}

9.8

package demo;

public class dijiuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
    Fan fan1 = new Fan();
    fan1.setSpeed(Fan.FAST);
    fan1.setRadius(10);
    fan1.setColor("yellow");
    fan1.setOn(true);
    Fan fan2 = new Fan();
    fan2.setSpeed(Fan.MEDIUM);
    System.out.println(fan1.toString());
    System.out.println(fan2.toString());
}

}

class Fan{

 	final static int SLOW=1;
    final static int MEDIUM=2;
    final static int FAST=3;
    private int speed = SLOW;
    private boolean on = false;
    private double radius = 5;
    private String color="blue";
    public int getSpeed(){
        return speed;
    }
    public void setSpeed(int s){
        speed=s;
    }
    public boolean getOn(){
        return on;
    }
    public void setOn(boolean j){
        on=j;
    }
    public double getRadius(){
        return radius;
    }
    public void setRadius(double r){
        radius=r;
    }
    public String getColor(){
        return color;
    }
    public void setColor(String s){
        color=s;
    }
    public Fan(){
        speed=SLOW;
        on=false;
        radius=5;
        color="blue";
    }
    public String toString(){
        String r="";
        if(this.on)
            r=r+speed+" "+color+" "+radius;
        else
            r=r+"fan is off "+color+" "+radius;
        return r;
    }

}

9.9

package demo;

public class dijiuzhang {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        RegularPolygon p = new RegularPolygon();
        RegularPolygon p2 = new RegularPolygon(6,4);
        RegularPolygon p3 = new RegularPolygon(10,4,5.6,7.8);
        System.out.println(p.getPerimeter()+" "+p.getArea());
        System.out.println(p2.getPerimeter()+" "+p2.getArea());
        System.out.println(p3.getPerimeter()+" "+p3.getArea());
    }

}

class RegularPolygon{

 	private int n=3;
    private double side=1;
    private double x=0;
    private double y=0;
    public RegularPolygon(){
        n=3;
        side=1;
    }
    public RegularPolygon(int num,double len){
        n=num;
        side=len;
    }
    public RegularPolygon(int hum,double len,double x,double y){
        n=hum;
        side=len;
        x=x;
        y=y;
    }
    public void setN(int n){
        n=n;
    }
    public int getN(){
        return n;
    }
    public void setSide(double s){
        side=s;
    }
    public double getSide(){
        return side;
    }
    public void setX(double x){
        x=x;
    }
    public double getX(){
        return x;
    }
    public void setY(double y){
        y=y;
    }
    public double getY(){
        return y;
    }
    public double getPerimeter(){
        return n*side;
    }
    public double getArea(){
        return n*side*side/(4*Math.tan(Math.PI/n));
    }

}

9.10

package demo;

import java.util.Scanner;

public class dijiuzhang {

public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a b c: ");
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        QuadraticEquation r = new QuadraticEquation(a,b,c);
        if(r.getDiscriminant()<0)
            System.out.println("The equation has no roots.");
        else if(r.getDiscriminant()==0)
            System.out.println("One root "+r.getRoot1());
        else
            System.out.println("Two roots "+r.getRoot1()+" "+r.getRoot2());
}

}

class QuadraticEquation{

  	private double a;
    private double b;
    private double c;
    public QuadraticEquation(double a1,double b1,double c1){
        a=a1;
        b=b1;
        c=c1;
    }
    public double getA(){
        return a;
    }
    public double getB(){
        return b;
    }
    public double getC(){
        return c;
    }
    public double getDiscriminant(){
        return b*b-4*a*c;
    }
    public double getRoot1(){
        return (-1*b+Math.sqrt(b*b-4*a*c))/(2*a);
    }
    public double getRoot2(){
        return (-1*b-Math.sqrt(b*b-4*a*c))/(2*a);
    }

}

9.11

package demo;

import java.util.Scanner;

public class dijiuzhang {

public static void main(String[] args) {
		// TODO Auto-generated method stub
        System.out.print("Enter a b c d e f: ");
        Scanner input = new Scanner(System.in);
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        double d = input.nextDouble();
        double e = input.nextDouble();
        double f = input.nextDouble();
        LinearEquation r = new LinearEquation(a,b,c,d,e,f);
        if(r.isSolvable())
            System.out.println(r.getX()+" "+r.getY());
        else
            System.out.println("The equation has no solutions.");
}

}

class LinearEquation{

    private double a,b,c,d,e,f;
    public LinearEquation(double a1,double b1,double c1,double d1,double e1,double f1){
        a=a1;
        b=b1;
        c=c1;
        d=d1;
        e=e1;
        f=f1;
    }
    public double getA(){
        return a;
    }
    public double getB(){
        return b;
    }
    public double getC(){
        return c;
    }
    public double getD(){
        return d;
    }
    public double getE(){
        return e;
    }
    public double getF(){
        return f;
    }
    public boolean isSolvable(){
        return a*d-b*c!=0;
    }
    public double getX(){
        return (e*d-b*f)/(a*d-b*c);
    }
    public double getY(){
        return (a*f-e*c)/(a*d-b*c);
    }

}

9.12

package demo;

import java.util.Scanner;

public class dijiuzhang {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the four points x1 y1 x2 y2 x3 y3 x4 y4: ");
        double x1=input.nextDouble();
        double y1=input.nextDouble();
        double x2=input.nextDouble();
        double y2=input.nextDouble();
        double x3=input.nextDouble();
        double y3=input.nextDouble();
        double x4=input.nextDouble();
        double y4=input.nextDouble();
        LinearEquation j = new LinearEquation(y1-y2,x2-x1,y3-y4,x4-x3,(y1-y2)*x1-(x1-x2)*y1,(y3-y4)*x3-(x3-x4)*y3);
        if(j.isSolvable())
            System.out.println(j.getX()+" "+j.getY());
        else
            System.out.println("No joining.");
    }

}

class LinearEquation{

    private double a,b,c,d,e,f;
    public LinearEquation(double a1,double b1,double c1,double d1,double e1,double f1){
        a=a1;
        b=b1;
        c=c1;
        d=d1;
        e=e1;
        f=f1;
    }
    public double getA(){
        return a;
    }
    public double getB(){
        return b;
    }
    public double getC(){
        return c;
    }
    public double getD(){
        return d;
    }
    public double getE(){
        return e;
    }
    public double getF(){
        return f;
    }
    public boolean isSolvable(){
        return a*d-b*c!=0;
    }
    public double getX(){
        return (e*d-b*f)/(a*d-b*c);
    }
    public double getY(){
        return (a*f-e*c)/(a*d-b*c);
    }

}

9.13

package demo;

import java.util.Scanner;

public class dijiuzhang {

public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of rows and columns in the array: ");
        int rows = input.nextInt();
        int columns = input.nextInt();
        double[][] n = new double[rows][columns];
        System.out.print("Enter the array: ");
        for(int i=0;i<rows;i++){
            for(int j=0;j<columns;j++)
                n[i][j]=input.nextDouble();
        }
        Location location = locateLargest(n);
        System.out.println("The location of the largest element is " + location.maxValue+" at (" + location.row+", " + location.column+")");
    }
    public static Location locateLargest(double[][] a){
        Location max = new Location();
        max.maxValue=a[0][0];
        max.column=0;
        max.row=0;
        int rowCount = a.length;
        int columnCount = a[0].length;
        for(int i=0;i<rowCount;i++){
            for(int j=0;j<columnCount;j++){
                if(a[i][j]>max.maxValue){
                	max.row=i;
                	max.maxValue=a[i][j];
                	max.column=j;
                }
            }
        }
        return max;
}

}

class Location{

    public int row;
    public int column;
    public double maxValue;

}

第九章习题 完

1.1 单项选择题 1. 数据结构是一门研究非数值计算的程序设计问题中,数据元素的① 、数据信息在计算机中的② 以及一组相关的运算等的课程。 ① A.操作对象   B.计算方法  C.逻辑结构  D.数据映象 ② A.存储结构 B.关系 C.运算 D.算法 2. 数据结构DS(Data Struct)可以被形式地定义为DS=(D,R),其中D是① 的有限集合,R是D上的② 有限集合。 ① A.算法 B.数据元素 C.数据操作 D.数据对象 ② A.操作 B.映象 C.存储 D.关系 3. 在数据结构中,从逻辑上可以把数据结构分成 。 A.动态结构和静态结构 B.紧凑结构和非紧凑结构 C.线性结构和非线性结构 D.内部结构和外部结构 4. 算法分析的目的是① ,算法分析的两个主要方面是② 。 ① A. 找出数据结构的合理性 B. 研究算法中的输入和输出的关系 C. 分析算法的效率以求改进 D. 分析算法的易懂性和文档性 ② A. 空间复杂性和时间复杂性 B. 正确性和简明性 C. 可读性和文档性 D. 数据复杂性和程序复杂性 5. 计算机算法指的是① ,它必具备输入、输出和② 等五个特性。 ① A. 计算方法 B. 排序方法 C. 解决问题的有限运算序列 D. 调度方法 ② A. 可行性、可移植性和可扩充性 B. 可行性、确定性和有穷性 C. 确定性、有穷性和稳定性 D. 易读性、稳定性和安全性 1.2 填空题(将正确的答案填在相应的空中) 1. 数据逻辑结构包括 、 、 和 四种类型,树形结构和图形结构合称为 。 2. 在线性结构中,第一个结点 前驱结点,其余每个结点有且只有 个前驱结点;最后一个结点 后续结点,其余每个结点有且只有 个后续结点。 3. 在树形结构中,树根结点没有 结点,其余每个结点有且只有 个直接前驱结点,叶子结点没有 结点,其余每个结点的直接后续结点可以 。 4. 在图形结构中,每个结点的前驱结点数和后续结点数可以 。 5. 线性结构中元素之间存在 关系,树形结构中元素之间存在 关系,图形结构中元素之间存在 关系。 6. 算法的五个重要特性是__ __ , __ __ , ___ _ , __ __ , _ ___。 7. 分析下面算法(程序段),给出最大语句频度 ,该算法的时间复杂度是__ __。 for (i=0;i<n;i++) for (j=0;j<n; j++) A[i][j]=0; 8. 分析下面算法(程序段),给出最大语句频度 ,该算法的时间复杂度是__ __。 for (i=0;i<n;i++) for (j=0; j<i; j++) A[i][j]=0; 9. 分析下面算法(程序段),给出最大语句频度 ,该算法的时间复杂度是__ __。 s=0; for (i=0;i<n;i++) for (j=0;j<n;j++) for (k=0;k<n;k++) s=s+B[i][j][k]; sum=s; 10. 分析下面算法(程序段)给出最大语句频度 ,该算法的时间复杂度是__ __。 int i=0,s=0; while (s<n) { i++; s+=i; //s=s+i } 11. 分析下面算法(程序段)给出最大语句频度 ,该算法的时间复杂度是__ __。 i=1; while (i<=n) i=i*2;
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xupengboo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值