java面向對象編程示例(java面向對象程序設計筆記)

本文目錄一覽:

java面向對象編程

建個數據庫學生表,java鏈接數據庫對其進行操作,很多方法可以實現(mybatis,ibatis,jdbc),顯示在頁面就可以了

java中面向對象的例子

這是我的理解 僅供參考:

類好比就是人裏面有男人、女人;動物裏面有牛、羊、狗、貓等等;而這裡所說的人就是一類,動物是另外一類 ;總不能把人和動物算成一類吧(當然不算高級動物);而對象就是我說的男人、女人、還有動物里的牛、羊、狗、貓等等;它們每一個都算一個對象。是實實在在存在的;面向對象就是以這些對象為中心來寫的程序,不管你寫的是什麼功能,都必須有對象去對這個功能進行操作才行;好比你製造出了一輛車 ,必須要有人去開對吧,如果這個車沒人開 那它就是個廢品。它永遠都不會動。不知道我這樣說你是否理解。

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();

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(“分數為[” + 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的遍歷方式。

運行結果:

啊 代碼又一坨的的擠在一起了,百度知道這個真是無語,我把幾個關鍵的地方截個圖給你康康:

ScoreArray.java

成員變量和構造函數

忘了說了,構造的同時還統計了有效分數(0~100)的數量

求最大值

冒泡排序後取最大值

求最小值

冒泡排序後取最小值

求均值

利用java 的BigDecimal類解決除法運算精度的問題,四捨五入並保留了兩位小數

排序

就是冒泡排序,從小到大

靜態的文本說明

StudentScoreArray.java:

繼承

分數分佈統計

注意我說的map那裡

靜態說明文本

Test1.java:

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

Java面向對象編程

舉個簡單的例子:public static void main(String [] args) {

Scanner sc = new Scanner(System.in);

System.out.println(“請輸入你的年齡:”);

int age = sc.nextInt();

System.out.println(“請輸入你的姓名:”);

String name = sc.nextLine();

System.out.println(“請輸入你的工資:”);

float salary = sc.nextFloat();

System.out.println(“你的信息如下:”);

System.out.println(“姓名:”+name+”\n”+”年齡:”+age+”\n”+”工資:”+salary);

}

在哪個包,按快捷鍵引入包就知道了

Java面向對象程序編程

public static void main(String args[]) {

    Scanner scanner = new Scanner(System.in);

    System.out.println(“請輸入一個開始數字:”);

    int a = 0;

    try {

        a = scanner.nextInt();

    } catch (Exception e) {

        System.out.println(“輸數字不合法”);        

        return;

    }

    System.out.println(“請輸入一個結束數字:”);

    int b = 0;    

    try {

        b = scanner.nextInt();

    } catch (Exception e) {

        System.out.println(“輸數字不合法”);        

        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);

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/302003.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-30 16:10
下一篇 2024-12-30 16:10

相關推薦

發表回復

登錄後才能評論