1. 程式人生 > >java語言程式設計基礎篇第九章程式設計練習題

java語言程式設計基礎篇第九章程式設計練習題

1

import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		Rectangle R1 = new Rectangle(40,4);
		Rectangle R2 = new Rectangle(35.9,3.5);
		System.out.println(R1.getArea() + "\n" + R2.getArea());
		System.out.println(R1.getPerimeter() + "\n" + R2.getPerimeter());
	}
}

class Rectangle{
	double width = 1;
	double height = 1;
	Rectangle() {
	}
	
	Rectangle(double h, double w){
		height = h;
		width = w;
	}
	
	double getArea(){
		return width*height;
	}
	
	double getPerimeter(){
		return 2*(width+height);
	}
	
}

2

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Stock obj = new Stock("ORCL","Oracle Corporation");
		obj.previousClosingPrice = 34.5;
		obj.currentPrice = 34.35;
		System.out.println(obj.getChangePercent());
	}
}

class Stock{
	String symbol;
	String name;
	double previousClosingPrice = 0.0;
	double currentPrice = 0.0;
	
	public Stock(String sym, String na){
		symbol = sym;
		name = na;
	}
	
	public String getChangePercent(){
		return Math.abs(((currentPrice-previousClosingPrice)/previousClosingPrice))*100+"%";
	}
}

3

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

public class Main{
	
	public static void main(String[] args){
		long num = 10000;
		Date date = new Date(num);
		System.out.println(date.toString());
		
		date.setTime(num*10);
		System.out.println(date.toString());
		
		date.setTime(num*100);
		System.out.println(date.toString());
		
		date.setTime(num*1000);
		System.out.println(date.toString());
		
		date.setTime(num*num);
		System.out.println(date.toString());
		
		date.setTime(num*num*10);
		System.out.println(date.toString());
		
		date.setTime(num*num*100);
		System.out.println(date.toString());
		
		date.setTime(num*num*1000);
		System.out.println(date.toString());
	}
}

4

import java.util.Scanner;
import java.util.Random;

public class Main{
	
	public static void main(String[] args){
		Random random = new Random(1000);
		for(int i = 0; i < 50; ++i){
			System.out.print(random.nextInt(100) + " ");
			if(i%10 == 0)
				System.out.println();
		}
	}
}

5

import java.util.Scanner;
import java.util.GregorianCalendar;
//我很納悶為什麼輸出結果是一樣的
public class Main{
	
	public static void main(String[] args){
		GregorianCalendar calendar = new GregorianCalendar();
		System.out.println(calendar.YEAR + ":" + calendar.MONTH + ":" + calendar.DAY_OF_MONTH);

		calendar.setTimeInMillis(1234567898765L);
		System.out.println(calendar.YEAR + ":" + calendar.MONTH + ":" + calendar.DAY_OF_MONTH);
	}
}

6

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

public class Main{
	
	public static void main(String[] args){
		StopWatch time = new StopWatch();
		int[] nums = new int[100000];
		for(int i = 1; i <= 100000; ++i)
			nums[i-1] = (int)((System.currentTimeMillis()/i)%1000);
		
		time.start();
		for(int i = 0; i < 99999; ++i){
			for(int j = i+1; j < 100000; ++j){
				if(nums[i] > nums[j]){
					int temp = nums[i];
					nums[i] = nums[j];
					nums[j] = temp;
				}
			}
		}
		time.stop();
		
		System.out.println(time.getElapsedTime());
	}
}

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

7

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

public class Main{
	
	public static void main(String[] args){
		Account account = new Account(1122,20000);
		if(account.withDraw(2500))
			System.out.println("Success");
		else
			System.out.println("Fail");
		
		if(account.deposit(2500))
			System.out.println("Success");
		else
			System.out.println("Fail");
		
		System.out.println("balance:" + account.getBalance());
		System.out.println("monthly interest:" + account.getMonthlyInterestRate()*account.getBalance());
		System.out.println(account.getDate().toString());
	}
}

class Account{
	private int id;
	private double balance; 
	private double annualInterestRate;
	private Date dateCreated;
	
	public Account(){
		dateCreated = new Date();
		id = 0;
		balance = 0;
		annualInterestRate = 0;
	}
	
	public Account(int ID, double BALANCE){
		dateCreated = new Date();
		id = ID;
		balance = BALANCE;
		annualInterestRate = 0.045;
	}
	
	public int getId(){
		return id;
	}
	
	public void setId(int ID){
		id = ID;
	}
	
	public double getBalance(){
		return balance;
	}
	
	public void setBalance(double ba){
		balance = ba;
	}
	
	public double getAnnualInterestRate(){
		return annualInterestRate;
	}
	
	public void setAnnualInterestRate(double annual){
		annualInterestRate = annual;
	}
	
	public Date getDate(){
		return dateCreated;
	}
	
	public double getMonthlyInterestRate(){
		return annualInterestRate/12.0;
	}
	
	public boolean withDraw(double money){
		if(balance >= money){
			balance -= money;
			return true;
		}
		else
			return false;
	}
	
	public boolean deposit(double money){
		if(money >= 0){
			balance += money;
			return true;
		}
		else
			return false;
	}
}

8

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

public class Main{
	
	public static void main(String[] args){
		Fan fan1 = new Fan();
		fan1.setSpeed();
		fan1.setRadius(10);
		fan1.setColor("yellow");
		fan1.setOn();
		Fan fan2 = new Fan();
		fan2.setSpeed();
		System.out.println(fan1.toString());
		System.out.println(fan2.toString());
	}
}

class Fan{
	public static final int SLOW = 1;
	public static final int MEDIUM = 2;
	public static final int FAST= 3;
	
	private int speed;
	private boolean on;
	private double radius;
	private String color;
	
	public Fan(){
		speed = SLOW;
		on = false;
		radius = 5;
		color = "blue";
	}
	
	public int getSpeed(){
		return speed;
	}
	
	public void setSpeed(){
		System.out.println("1.SLOW    2.MEDIUM    3.FAST");
		Scanner cin = new Scanner(System.in);
		int num = cin.nextInt();
		speed = num;
	}
	
	public boolean getOn(){
		return on;
	}
	
	public void setOn(){
		System.out.println("1.on     2.off");
		Scanner cin = new Scanner(System.in);
		int num = cin.nextInt();
		if(num == 1)
			on = true;
		else
			on = false;
	}
	
	public double getRadius(){
		return radius;
	}
	
	public void setRadius(double r){
		radius = r;
	}
	
	public String getColor(){
		return color;
	}
	
	public void setColor(String str){
		color = str;
	}
	
	public String toString(){
		if(on)
			return "Speed:" + speed + "\nColor:" + color + "\nRadius:" + radius;
		else
			return "fan is off";
	}
}

9

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		RegularPolygon g1 = new RegularPolygon();
		System.out.println("g1 perimeter:" + g1.getPerimeter() + "    " + "g1 area:" + g1.getArea());
		RegularPolygon g2 = new RegularPolygon(6,4);
		System.out.println("g2 perimeter:" + g2.getPerimeter() + "    " + "g2 area:" + g2.getArea());
		RegularPolygon g3 = new RegularPolygon(10,4,5.6,7.8);
		System.out.println("g3 perimeter:" + g3.getPerimeter() + "    " + "g3 area:" + g3.getArea());
	}
}

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

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		QuadraticEquation expr = new QuadraticEquation(1,4,4);
		if(expr.getRoot1() == 0 && expr.getRoot2() == 0){
			System.out.println("The equation has no roots");
		}
		else if(expr.getRoot1() == expr.getRoot2())
			System.out.println("R : " + expr.getRoot1());
		else
			System.out.println("R1 : " + expr.getRoot1() + "    " + "R2 :" + expr.getRoot2());
	}
}

class QuadraticEquation{
	private double a,b,c;
	
	QuadraticEquation(double x, double y, double z){
		a = x;
		b = y;
		c = z;
	}
	
	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(){
		double judge = this.getDiscriminant();
		if(judge >= 0){
			return (-b + Math.sqrt(judge))/(2*a);
		}
		else
			return 0;
	}
	
	public double getRoot2(){
		double judge = this.getDiscriminant();
		if(judge >= 0){
			return (-b + Math.sqrt(judge))/(2*a);
		}
		else
			return 0;
	}
}

11

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		LinearEquation expr1 = new LinearEquation(9.0,4.0,3.0,-5.0,-6.0,-21.0);
		LinearEquation expr2 = new LinearEquation(1.0,2.0,2.0,4.0,4.0,5.0);
		
		if(expr1.isSolvable()){
			System.out.println("x:" + expr1.getX() + "  " + "y:" + expr1.getY());
		}
		else
			System.out.println("The equation has no solution");
		
		if(expr2.isSolvable()){
			System.out.println("x:" + expr2.getX() + "  " + "y:" + expr2.getY());
		}
		else
			System.out.println("The equation has no solution");
	}
}

class LinearEquation{
	private double a,b,c,d,e,f;
	
	public LinearEquation(double v1, double v2, double v3, double v4, double v5, double v6){
		a = v1;
		b = v2;
		c = v3;
		d = v4;
		e = v5;
		f = v6;
	}
	
	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(){
		if((a*d-b*c) != 0)
			return true;
		else
			return false;
	}
	
	public double getX(){
		return (e*d-b*f)/(a*d-b*c);
	}
	
	public double getY(){
		return (a*f-e*c)/(a*d-b*c);
	}
}

12

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		double x1,y1,x2,y2,x3,y3,x4,y4;
		System.out.print("Enter x1,y1,x2,y2,x3,y3,x4,y4:");
		x1 = cin.nextDouble();
		y1 = cin.nextDouble();
		x2 = cin.nextDouble();
		y2 = cin.nextDouble();
		x3 = cin.nextDouble();
		y3 = cin.nextDouble();
		x4 = cin.nextDouble();
		y4 = cin.nextDouble();
		double a,b,c,d,e,f;
		a = y1 - y2;
		b = -(x1 - x2);
		c = y3 - y4;
		d = -(x3 - x4);
		e = (y1-y2)*x1 - (x1-x2)*y1;
		f = (y3-y4)*x3 - (x3-x4)*y3;
		LinearEquation expr = new LinearEquation(a,b,c,d,e,f);
		
		if(expr.isSolvable()){
			System.out.println("x:" + expr.getX() + "  " + "y:" + expr.getY());
		}
		else
			System.out.println("The tow line are parallel");
	}
}

class LinearEquation{
	private double a,b,c,d,e,f;
	
	public LinearEquation(double v1, double v2, double v3, double v4, double v5, double v6){
		a = v1;
		b = v2;
		c = v3;
		d = v4;
		e = v5;
		f = v6;
	}
	
	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(){
		if((a*d-b*c) != 0)
			return true;
		else
			return false;
	}
	
	public double getX(){
		return (e*d-b*f)/(a*d-b*c);
	}
	
	public double getY(){
		return (a*f-e*c)/(a*d-b*c);
	}
}

13

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		double[][] nums = new double[3][4];
		Scanner cin = new Scanner(System.in);
		for(int i = 0; i < 3; ++i){
			for(int j = 0; j < 4; ++j){
				nums[i][j] = cin.nextDouble();
			}
		}
		
		Location location = locateLargest(nums);
		System.out.println(location.maxValue + "--" + location.row + "," + location.column);
	}
	
	public static Location locateLargest(double[][] a){
		int x = 0,y = 0;
		double max = 0;
		for(int i = 0; i < a.length; ++i){
			for(int j = 0; j < a[i].length; ++j){
				if(a[i][j] > max){
					max = a[i][j];
					x = i;
					y = j;
				}
			}
		}
		Location temp = new Location();
		temp.row = x;
		temp.column = y;
		temp.maxValue = max;
		return temp;
	}
}

class Location{
	public int row,column;
	public double maxValue;
}


相關推薦

java語言程式設計基礎程式設計練習題

1 import java.util.Scanner; public class Main{ public static void main(String[] args){ Rectangle R1 = new Rectangle(40,4); Rectangl

java語言程式設計基礎程式設計練習題

1 import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); dou

java語言程式設計基礎程式設計練習題

1 import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); doubl

java語言程式設計基礎程式設計練習題

1 import java.util.Scanner; public class Main{ public static void main(String[] args){ final int NUMBER = 100; int count = 0; Sc

java語言程式設計基礎三題】

package OctTTYwo; public class MyInteger { int value ; String valuel; public MyIntege

java語言程式設計(基礎)十版程式設計練習題[1.9]

(矩形的面積和周長)編寫程式,使用以下公式計算並顯示寬度為4.5、高度為7.9的矩形的面積和周長。 面積 = 寬 × 高 /* * To change this license header, choose License Headers in Project Propertie

Java編程基礎

end 調用 基本類 進棧 多態 靜態成員 好處 函數賦值 構造 構造方法 一:概念:   給對象的數據(屬性)進行初始化 二:特點:   a.方法名與類同名(字母大小寫也要一樣)   b.沒有返回值類型   c.沒有具體的返回值 return 三:構造方法重載:   方法

C語言程式設計 現代方法 程式設計題答案

以下程式都是在VS軟體下進行編譯的,如果要用VC編譯器,刪除system(“paues”);即可 9.1 //No.1不用遞迴 #include<stdio.h> #define N 20 void selection_sort(int k, int c[]); int ma

java語言程式設計基礎十二程式設計練習題

1 package yongheng; import java.util.Scanner; public class Calculator { public static void main(String[] args) { Sca

Java語言程式設計基礎十版第一程式設計練習題答案

首先第一章是初步的認識java的,其中的練習題非常簡單,有語言基礎功底的應該問題不大。我挑了幾個可能會有問題或者是帶號的例子來講解一下。所有的程式碼都已經經過我本人驗證。* 如果有其他題目模糊不清的歡

java語言程式設計基礎十一程式設計練習題

1 package yongheng; import java.util.Scanner; public class Main { public static void main(String agrs[]){ Triangle t

Java程式設計基礎8手記Part 1

本章主要內容 Part 1 - 子類的建立 - 在子類中訪問父類的成員 - 覆蓋父類的方法 - …… 本章主要講繼承、抽象類和介面,這篇部落格講的是繼承這一部分。使用實驗的程式碼做例子來說明。 類的繼承 類的繼承是面向物件的程式設計的一

java程式設計思想(

java中介面與內部類為我們提供了一種使介面與實現分離更加結構化的方法。 1、抽象方法與抽象類 抽象方法:就是一種不完整的方法,只有宣告沒有方法體。抽象方法結構如下: abstract void function(); 抽象類:含有一個或者多個抽象方法的

Java-Web學習筆記(

port pac clas tle code lang tran RR req 一:自定義標簽庫(步驟)1>開發自定義標簽類(編寫一個實現SimpleTagSupport接口的java類) package book07; import java.io.IOExcep

Vue架構【基礎-02】:介紹

介紹 一、Vue.js是什麼 Vue是一套用於構建使用者介面的漸進式框架。與其他大型框架不同的是,Vue被設計為可以自底向上逐層應用。Vue的核心庫只關注檢視層,不僅易於上手,還便於與第三方庫或既有專案整合。另一方面,當與現代化的工具鏈以及各種支援類庫結合使用時,Vue也完全能夠為複雜的單頁應用提供驅動。

Vue架構【基礎-04】:模板語法

模板語法 Vue.js使用了基於HTML的模板語法,允許開發者宣告式的將DOM繫結至底層Vue例項的資料。所有Vue.js的模板都是合法的HTML,所以能被遵循規範的瀏覽器和HTML解析器解析。 在底層的實現上,Vue將模板編譯成虛擬DOM渲染函式,結合響應系統,Vue能夠智慧的計算出最少需要重新渲染多少

Vue架構【基礎-06】:class和style繫結

class和style繫結 操作元素的 class 列表和內聯樣式是資料繫結的一個常見需求。因為它們都是屬性,所以我們可以用 v-bind 處理它們:只需要通過表示式計算出字串結果即可。不過,字串拼接麻煩且易錯。因此,在將 v-bind 於 class&nb

Vue架構【基礎-07】:條件渲染

條件渲染 一、v-if 在字串模板中,比如 Handlebars,我們得像這樣寫一個條件塊: <!-- Handlebars 模板 --> {{#if ok}} <h1>Yes</h1> {{/if}} 在 Vue 中,我們使用 v

程式設計技術》例程

《程式設計技術》例程 《程式設計技術》第一章 C語言與程式設計(例程) 《程式設計技術》第二章 C語言基礎知識(例程) 《程式設計技術》第三章 程式控制結構(例程) 《程式設計技術》第四章 陣列和字串(例程) 《程式設計技術》第五章 指標(例程) 《程式設計技術》第六章 函式(例程) 《

C Primer Plus(6版)程式設計練習答案

9.11程式設計練習 /* PE 9-1 設計一個函式min(x, y),返回兩個double型別值的較小值,在一個簡單的驅動程式中測試該函式 */ #include <stdio.h> double min(double a, double b