代碼如下:
成都創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站設計、網(wǎng)站制作與策劃設計,芮城網(wǎng)站建設哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設10余年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:芮城等地區(qū)。芮城做網(wǎng)站價格咨詢:18982081108
abstract?class?Worker?{
protected?String?name;
public?Worker(String?name)?{
this.name?=?name;
}
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?abstract?void?introduce();
}
public
class
Person
{
//(1)
設計一個類
人員,包含三個成員變量:姓名、年齡、性別。
private
String
name;
private
int
age;
private
String
sex;
public
String
getName()
{
return
name;
}
public
void
setName(String
name)
{
this.name
=
name;
}
public
int
getAge()
{
return
age;
}
public
void
setAge(int
age)
{
this.age
=
age;
}
public
String
getSex()
{
return
sex;
}
public
void
setSex(String
sex)
{
this.sex
=
sex;
}
}
//(2)
設計員工和顧客兩個類為
人員類的子類,員工類包含一個成員變量:職稱級別;顧客包含一個成員變量:賬戶余額。
class
worker
extends
Person{
private
String
Title_Level;
public
String
getTitle_Level()
{
return
Title_Level;
}
public
void
setTitle_Level(String
title_Level)
{
Title_Level
=
title_Level;
}
}
class
Customer
extends
Person{
private
int
Account_balance;
public
int
getAccount_balance()
{
return
Account_balance;
}
public
void
setAccount_balance(int
account_balance)
{
Account_balance
=
account_balance;
}
}
//(3)
設計一個類
取款機包含兩個成員方法:取款,查詢賬戶。
interface
ATM{
void
Teller(Customer
cus);
void
Checkaccount(Customer
cus);
}
//4)
實現(xiàn)取款機的取款方法,
//當顧客來取錢但是賬戶余額不足時拋出異?!坝囝~不足”并顯示,
//無論取款是否發(fā)生異常,最終取款方法需要執(zhí)行“退卡”操作。
class
ATMimpl
implements
ATM{
public
void
Checkaccount(Customer
cus)
{
}
public
void
Teller(Customer
cus)
{
}
}
public?static?void?main(String?args[])?{
Scanner?scanner?=?new?Scanner(System.in);
System.out.println("請輸入一個開始數(shù)字:");
int?a?=?0;
try?{
a?=?scanner.nextInt();
}?catch?(Exception?e)?{
System.out.println("輸數(shù)字不合法");????????
return;
}
System.out.println("請輸入一個結束數(shù)字:");
int?b?=?0;????
try?{
b?=?scanner.nextInt();
}?catch?(Exception?e)?{
System.out.println("輸數(shù)字不合法");????????
return;
}????
int?sum?=?0;????
if?(a??b)?{????????
for?(int?i?=?b;?i?=?a;?i++)?{
sum?=?sum?+?i;
}
}?else?{??????
for?(int?i?=?a;?i?=?b;?i++)?{
sum?=?sum?+?i;
}
}
System.out.println(sum);
}
import java.util.Scanner;
public class Demo
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
Point p1,p2;
System.out.println("請輸入第1個點的x、y坐標:");
p1=new Point(sc.nextDouble(),sc.nextDouble());
System.out.println("請輸入第2個點的x、y坐標:");
p2=new Point(sc.nextDouble(),sc.nextDouble());
System.out.println("點"+p1+"與點"+p2+"的距離是"+p1.distance(p2));
}
}
class Point
{
Point(double x,double y)
{
this.x=x;
this.y=y;
}
public String toString()
{
return "("+x+","+y+")";
}
double distance(Point p)
{
return Math.sqrt(Math.pow(this.x-p.x,2)+Math.pow(this.y-p.y,2));
}
private double x,y;
}
一共三個類:ScoreArray.java、StudentScoreArray.java和Test1.java,具體為:
public class ScoreArray {
private int[] scores;
private int scoreCount;
public int[] getScores() {
return scores;
}
public int getScoreCount() {
return scoreCount;
}
//構造函數(shù)
public ScoreArray(int[] scores) {
this.scores = scores;
for (int score : scores) {
if (score = 0 score = 100) {
this.scoreCount++;
}
}
}
//求最大值
public int getMax() {
int[] scores = this.scores;
int temp;
for (int i = 0; i scores.length; i++) {
for (int j = 0; j scores.length - 1 - i; j++) {
if (scores[j] scores[j + 1]) {
temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
}
}
}
return scores[scores.length - 1];
}
//求最小值
public int getMin() {
int[] scores = this.scores;
int temp;
for (int i = 0; i scores.length; i++) {
for (int j = 0; j scores.length - 1 - i; j++) {
if (scores[j] scores[j + 1]) {
temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
}
}
}
return scores[0];
}
//求均值
public double getAvg() {
int sum = 0;
for (int score : this.scores) {
sum += score;
}
return new BigDecimal(sum).divide(
new BigDecimal(this.scores.length),
2, BigDecimal.ROUND_HALF_UP).doubleValue();
}
//排序
public void sort() {
int temp;
for (int i = 0; i this.scores.length; i++) {
for (int j = 0; j this.scores.length - 1 - i; j++) {
if (this.scores[j] this.scores[j + 1]) {
temp = this.scores[j];
this.scores[j] = this.scores[j + 1];
this.scores[j + 1] = temp;
}
}
}
}
//靜態(tài)說明類
public static void explain() {
System.out.println("本類[ScoreArray]實現(xiàn)了數(shù)組的:求最值[getMax()]、求均值[getAvg()]和排序[sort()]方法");
}
}
public class StudentScoreArray extends ScoreArray {
public StudentScoreArray(int[] scores) {
super(scores);
}
//統(tǒng)計
public void statistic() {
super.sort();
MapInteger, Integer map = new LinkedHashMap();
for (int i : super.getScores()) {
if (map.containsKey(i)) {
map.put(i, map.get(i) + 1);
} else {
map.put(i, 1);
}
}
map.forEach((k, v) - System.out.println("分數(shù)為[" + k + "]的人數(shù)為:[" + v + "]"));
}
//靜態(tài)說明類
public static void explain() {
System.out.println("本類[StudentScoreArray]實現(xiàn)了數(shù)組的:求最值[getMax()]、求均值[getAvg()]、排序[sort()]和分布統(tǒng)計[statistic()]方法");
}
}
public class Test1 {
public static void main(String[] args) {
int[] scores = {59, 60, 82, 58, 71, 99, 0, 59, 65};
ScoreArray scoreArray = new ScoreArray(scores);
ScoreArray.explain();
System.out.print("數(shù)組內(nèi)容:[");
for (int i : scoreArray.getScores()) {
System.out.print(i + " ");
}
System.out.println("]");
System.out.println("有效值個數(shù):" + scoreArray.getScoreCount());
System.out.println("最大值:" + scoreArray.getMax());
System.out.println("最小值:" + scoreArray.getMin());
System.out.println("平均值:" + scoreArray.getAvg());
scoreArray.sort();
System.out.print("排序后數(shù)組內(nèi)容:[");
for (int i : scoreArray.getScores()) {
System.out.print(i + " ");
}
System.out.println("]");
System.out.println("");
System.out.println("========華麗的分割線========");
System.out.println("");
StudentScoreArray studentScoreArray = new StudentScoreArray(scores);
StudentScoreArray.explain();
System.out.print("數(shù)組內(nèi)容:[");
for (int i : studentScoreArray.getScores()) {
System.out.print(i + ",");
}
System.out.println("]");
System.out.println("有效值個數(shù):" + studentScoreArray.getScoreCount());
System.out.println("最大值:" + studentScoreArray.getMax());
System.out.println("最小值:" + studentScoreArray.getMin());
System.out.println("平均值:" + studentScoreArray.getAvg());
studentScoreArray.sort();
System.out.print("排序后數(shù)組內(nèi)容:[");
for (int i : studentScoreArray.getScores()) {
System.out.print(i + " ");
}
System.out.println("]");
System.out.println("分數(shù)分布統(tǒng)計:");
studentScoreArray.statistic();
}
}
其中對StudentScoreArray類我要特別說明一下:
統(tǒng)計分布情況時,使用了Map,map是一種key-value的數(shù)據(jù)結構,其有個特點被我所利用:一個map中只能同時存在一個key,所以我以分數(shù)為key,以數(shù)量為value,遍歷分數(shù)數(shù)組時,如果是第一次遇到這個key(分數(shù)),則將其value(數(shù)量)置為1;如果已經(jīng)不是第一次遇見了,則將其value(數(shù)量)置為value + 1(數(shù)量 + 1)。另外需要遍歷這個map實現(xiàn)統(tǒng)計結果的打印,我這里使用了java8以后才支持的Lambda表達式,所以你要運行這個程序必須要使用jdk1.8以上的版本。如果你覺得這樣不妥,可以網(wǎng)上再搜一下map的遍歷方式。
運行結果:
啊 代碼又一坨的的擠在一起了,百度知道這個真是無語,我把幾個關鍵的地方截個圖給你康康:
ScoreArray.java
成員變量和構造函數(shù)
忘了說了,構造的同時還統(tǒng)計了有效分數(shù)(0~100)的數(shù)量
求最大值
冒泡排序后取最大值
求最小值
冒泡排序后取最小值
求均值
利用java 的BigDecimal類解決除法運算精度的問題,四舍五入并保留了兩位小數(shù)
排序
就是冒泡排序,從小到大
靜態(tài)的文本說明
StudentScoreArray.java:
繼承
分數(shù)分布統(tǒng)計
注意我說的map那里
靜態(tài)說明文本
Test1.java:
測試用例都使用的數(shù)組:int[] scores = {59, 60, 82, 58, 71, 99, 0, 59, 65};
把 人 封裝成一個類Person,繼承這個類 變有了 人應該有的屬性
把 家庭成員關系封裝一個類Family,繼承這個類有了家庭關系的屬性
把工作封裝成一個接口Jop,繼承這個接口實現(xiàn)了工作的方法
由于Person 和 Family 屬性都是固定的,即每個人的情況基本都一樣所以封裝成類
Jop 則是因為 每個人的工作不一樣,所有做成接口,究竟是什么樣的工作讓這個人來實現(xiàn)
這里Person 和 Family需要的屬性并不多,所以我統(tǒng)一將他們封裝了一個類 Person 實現(xiàn)代碼如下:
public?class?XiaoHong?extends?Person?implements?Jop?{
public?XiaoHong(String?name,?String?sex,?String?Father,?String?Mother,
String?Son)?{
super(name,?sex,?Father,?Mother,?Son);
//?TODO?Auto-generated?constructor?stub
}
public?static?void?main(String[]?args)?{
XiaoHong?xiaohong?=?new?XiaoHong("小紅","女","李雷",?"韓梅梅",?"討厭啦!人家還小!");
System.out.println(xiaohong);
xiaohong.work();
}
@Override
public?void?work()?{
System.out.println("我是一個高中生");
}
}
/********************************************************************************/
public?class?Person?{
private?String?name?=?"I?Don't?Know";
private?String?sex?=?"I?Don't?Know";
private?String?Father?=?"I?Don't?Know";
private?String?Mother?=?"I?Don't?Know";
private?String?Son?=?"I?Don't?Know";
public?Person(String?name,?String?sex,?String?myFather,?String?myMother,
String?mySon)?{
super();
this.name?=?name;
this.sex?=?sex;
this.Father?=?myFather;
this.Mother?=?myMother;
this.Son?=?mySon;
}
public?String?getName()?{
return?name;
}
public?String?getSex()?{
return?sex;
}
public?String?getMyFather()?{
return?Father;
}
public?String?getMyMother()?{
return?Mother;
}
public?String?getMySon()?{
return?Son;
}
@Override
public?String?toString()?{
return?"Person?[name="?+?name?+?",?sex="?+?sex?+?",?Father="
+?Father?+?",?Mother="?+?Mother?+?",?Son="?+?Son
+?"]";
}
}
/******************************************************************************/
public?interface?Jop?{
public?void?work();
}