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/n/302003.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-30 16:10
下一篇 2024-12-30 16:10

相关推荐

  • 北化教务管理系统介绍及开发代码示例

    本文将从多个方面对北化教务管理系统进行介绍及开发代码示例,帮助开发者更好地理解和应用该系统。 一、项目介绍 北化教务管理系统是一款针对高校学生和教职工的综合信息管理系统。系统实现的…

    编程 2025-04-29
  • 面向对象编程、类和对象

    面向对象编程(Object-Oriented Programming, OOP)是一种编程方法,它将现实世界中的事物抽象为对象(Object),对象的属性和方法被封装成类(Clas…

    编程 2025-04-29
  • 选择大容量免费云盘的优缺点及实现代码示例

    云盘是现代人必备的工具之一,云盘的容量大小是选择云盘的重要因素之一。本文将从多个方面详细阐述使用大容量免费云盘的优缺点,并提供相应的实现代码示例。 一、存储空间需求分析 不同的人使…

    编程 2025-04-29
  • Python调字号: 用法介绍字号调整方法及示例代码

    在Python中,调整字号是很常见的需求,因为它能够使输出内容更加直观、美观,并且有利于阅读。本文将从多个方面详解Python调字号的方法。 一、内置函数实现字号调整 Python…

    编程 2025-04-29
  • Python计算机语言程序设计用法介绍

    Python是一种高级编程语言,其设计目的是让程序员能够在编写代码时更加关注算法的设计,而不必过多地考虑语言细节。Python被广泛应用于网站开发、数据科学、人工智能、机器学习等各…

    编程 2025-04-28
  • Corsregistry.a的及代码示例

    本篇文章将从多个方面详细阐述corsregistry.a,同时提供相应代码示例。 一、什么是corsregistry.a? corsregistry.a是Docker Regist…

    编程 2025-04-28
  • Python Flask系列完整示例

    Flask是一个Python Web框架,在Python社区中非常流行。在本文中,我们将深入探讨一些常见的Flask功能和技巧,包括路由、模板、表单、数据库和部署。 一、路由 Fl…

    编程 2025-04-28
  • 使用面向对象程序设计方法改写猜数字游戏Python程序

    本文将从以下多个方面对猜数字游戏程序功能要求,使用面向对象程序设计方法改写该程序Python做详细的阐述。 一、游戏规则 1、游戏开始时,程序随机生成一个 1 到 100 之间的整…

    编程 2025-04-28
  • 微信mac版历史版完整代码示例与使用方法

    微信是一款广受欢迎的即时通讯软件,为了方便用户在Mac电脑上也能使用微信,微信团队推出了Mac版微信。本文将主要讲解微信mac版历史版的完整代码示例以及使用方法。 一、下载微信ma…

    编程 2025-04-28
  • 使用Python读取微信步数的完整代码示例

    本文将从多方面详细介绍使用Python读取微信步数的方法,包括使用微信Web API和使用Python爬虫获取数据,最终给出完整的代码示例。 一、使用微信Web API获取微信步数…

    编程 2025-04-28

发表回复

登录后才能评论