導航:首頁 > 編程語言 > 面向對象編程代碼java

面向對象編程代碼java

發布時間:2024-05-29 05:03:36

java面向對象編程求幫忙

一共三個類: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;
}
//構造函數
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;
}
}
}
}
//靜態說明類
public static void explain() {
System.out.println("本類[ScoreArray]實現了數組的:求最值[getMax()]、求均值[getAvg()]和排序[sort()]方法");
}
}


public class StudentScoreArray extends ScoreArray {
public StudentScoreArray(int[] scores) {
super(scores);
}
//統計
public void statistic() {
super.sort();
Map<Integer, 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("分數為[" + k + "]的人數為:[" + v + "]"));
}
//靜態說明類
public static void explain() {
System.out.println("本類[StudentScoreArray]實現了數組的:求最值[getMax()]、求均值[getAvg()]、排序[sort()]和分布統計[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("數組內容:[");
for (int i : scoreArray.getScores()) {
System.out.print(i + " ");
}
System.out.println("]");
System.out.println("有效值個數:" + scoreArray.getScoreCount());
System.out.println("最大值:" + scoreArray.getMax());
System.out.println("最小值:" + scoreArray.getMin());
System.out.println("平均值:" + scoreArray.getAvg());
scoreArray.sort();
System.out.print("排序後數組內容:[");
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("數組內容:[");
for (int i : studentScoreArray.getScores()) {
System.out.print(i + ",");
}
System.out.println("]");
System.out.println("有效值個數:" + studentScoreArray.getScoreCount());
System.out.println("最大值:" + studentScoreArray.getMax());
System.out.println("最小值:" + studentScoreArray.getMin());
System.out.println("平均值:" + studentScoreArray.getAvg());
studentScoreArray.sort();
System.out.print("排序後數組內容:[");
for (int i : studentScoreArray.getScores()) {
System.out.print(i + " ");
}
System.out.println("]");
System.out.println("分數分布統計:");
studentScoreArray.statistic();
}
}


其中對StudentScoreArray類我要特別說明一下:

統計分布情況時,使用了Map,map是一種key-value的數據結構,其有個特點被我所利用:一個map中只能同時存在一個key,所以我以分數為key,以數量為value,遍歷分數數組時,如果是第一次遇到這個key(分數),則將其value(數量)置為1;如果已經不是第一次遇見了,則將其value(數量)置為value + 1(數量 + 1)。另外需要遍歷這個map實現統計結果的列印,我這里使用了java8以後才支持的Lambda表達式,所以你要運行這個程序必須要使用jdk1.8以上的版本。如果你覺得這樣不妥,可以網上再搜一下map的遍歷方式。


運行結果:

測試用例都使用的數組:int[] scores = {59, 60, 82, 58, 71, 99, 0, 59, 65};

Ⅱ JAVA璇璦 閲囩敤闈㈠悜瀵硅薄紼嬪簭璁捐℃濇兂緙栧啓紼嬪簭璁$畻 n!,瑕佹眰浣跨敤鎺ュ彛 鍜屾娊璞$被瀹炵幇銆傛ユ眰鍏ㄩ儴浠g爜

/**
*
*闃朵箻璁$畻鍣
*
*鎺ュ彛
*/
publicinterfaceFactorialImp{
//璁$畻闃朵箻n!鐨勫
longfactorial(intn);
}

/**
*
*闃朵箻璁$畻鍣
*
*鎶借薄綾緇ф壙FactorialImp
*/
{
/**
*瀹炵幇璁$畻闃朵箻n!鐨勫肩殑鏂規硶
*/
publiclongfactorial(intn){
returnmultiplicationCount(n,n-1);
}

/**
*澧炲姞鎶借薄鏂規硶--璁$畻涓ゆ暟鐩鎬箻
*
*@paramparam1Int
*@paramparam2Int
*@return涓ゆ暟鐩鎬箻鐨勭Н
*/
(longparam1Int,longparam2Int);

}


/**
*瀹炵幇闃朵箻璁$畻鍣ㄧ被
*
*/
{


@Override
longmultiplicationCount(longparam1Int,longparam2Int){
if(param2Int==1){
returnparam1Int;
}else{
returnmultiplicationCount(param1Int*param2Int,param2Int-1);
}

}

}

/**
*嫻嬭瘯綾
*
*/
publicclassTest{

publicstaticvoidmain(String[]args){
FatoriallocalFatorial=newFatorial();
System.out.println("100!="+localFatorial.factorial(10));
}

}

Ⅲ java面向對象編程題目

public class Rectangle
{
private double width;
private double height;

//無參構造器
public Rectangle()
{}
//有參構造器
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
//屬性的get和set方法定義
public void setWidth(double width)
{
this.width = width;
}

public double getWidth()
{
return this.width;
}

public void setHeight(double height)
{
this.height = height;
}
public double getHeight()
{
return this.height;
}
//計算周長的方法
private double getPerimeter()
{
return (width+height)*2;
}
//計算面積的方法
private double getArea()
{
return width*height;
}

public static void main(String[] args)
{
Rectangle rec = new Rectangle(3.6,5.8);
System.out.println("The perimeter of Rectangle is:"+rec.getPerimeter());
System.out.println("The area of Rectangle is:"+rec.getArea());
}
}

閱讀全文

與面向對象編程代碼java相關的資料

熱點內容
lua編譯有什麼用 瀏覽:350
買了伺服器如何架設 瀏覽:929
如何運用mex函數編譯c 瀏覽:896
24歲程序員倒在工作上 瀏覽:919
怎麼算梁的加密區 瀏覽:93
2016版office怎麼解壓 瀏覽:270
怎麼把安卓手機調的更暗 瀏覽:167
蘋果空間新演算法 瀏覽:91
android文字動畫效果 瀏覽:146
java調試命令 瀏覽:213
android子線程looper 瀏覽:782
linux安裝java7 瀏覽:189
單片機fdh 瀏覽:107
單片機原理與應用下載 瀏覽:590
順風車車主app在哪裡下載 瀏覽:235
雷石柏雲伺服器功率 瀏覽:102
全球服是什麼伺服器 瀏覽:237
感測器怎麼連接伺服器 瀏覽:705
大數學pdf 瀏覽:646
哪個app可以登記自己的藏書 瀏覽:89