综合任务1

需求:学生的成绩存放于一个矩阵,其中行表示学生,列表示科目。如:第 0 行表示第 0 个学生的数学、语文、英语成绩。要求:

  1. 进行学生成绩的随机生成, 区间为 [50, 100].
  2. 找出成绩最好、最差的同学。但有挂科的同学不参加评比.

分析:
① 一个成绩矩阵——一个二维数组(行号代表学生编号,列好代表各个科目);
② 成绩随机生成,需要一个产生随机数的函数;
③ 需要计算所有没有挂科同学的成绩;
④ 从没有挂科的同学中选出成绩最好的、最差的同学。
Java知识补充
变量的作用:保存内存中的数据。
数据类型的分类:

  • 基本数据类型:包括boolean,char,byte,short,int,long,folat,double.基本数据类型的变量保存的是数据的值,可以直接在栈中分配内存。
  • 引用数据类型:包括类引用类型、接口引用类型和数组引用类型。不管何种引用类型的变量,它们引用的都是对象。引用类型的变量,在内存中存储的是对象的存储地址,变量放在栈中,对象实例放在堆中,变量保存的是堆地址。
    内存简化结构


    注意:将基本类型变量和引用类型变量的值赋给另一个变量的区别,引用类型变量赋值给另一个变量时,是将该变量指向堆中的地址给了新的变量,也就是说,这两个变量会指向同一个内存空间,当改变其中一个属性值时,也就是改变了另一个变量所值的属性值。

用new关键字创建对象

当一个引用类型的变量被声明后,如果没有初始化,那它就不指向任何对象。Java语言用new关键字创建对象,它有以下作用:

  • 为对象分配内存空间,将对象的实例变量自动初始化为其变量类型的默认值。
  • 如果实例变量在声明时被显示初始化,那就把初始化值赋给实例变量。
  • 调用构造方法。
  • 返回对象的引用。

用到的类

java.util.Random:获取随机数
import java.util.Arrays:提供操作数组的方法
代码中:
Random tempRandom = new Random();//创建一个Random类的实例对象,并赋给tempRandom(这是一个引用变量)
tempRandom.nextInt(51);//产生一个[0,51)的随机整数,得到的值再加上50,就能保证取得的随机数是[50,100]的整数。

Arrays.toString()将一维数组转换成字符串;
Arrays.deepToString()将多维数组转换成字符串

package day05;import java.util.Arrays;
import java.util.Random;public class Task1 {/*** The entrance of the program.* * @param args*/public static void main(String args[]) {task1();}// Of main/*** Method unit test.*/private static void task1() {// Step 1. Generate the data with n students and m courses.// Set these values by myself.int n = 10;int m = 3;int lowerBound = 50;int upperBound = 101;int threshold = 65;// Here we have to use an object to generate random numbers.Random tempRandom = new Random();int[][] data = new int[n][m];for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {data[i][j] = lowerBound + tempRandom.nextInt(upperBound - lowerBound);} // Of j} // Of iSystem.out.println("The data is:\r\n" + Arrays.deepToString(data));// Step 2. Compute the total score of each student.int[] totalScore = new int[n];for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (data[i][j] < threshold) {totalScore[i] = 0;break;} // Of iftotalScore[i] += data[i][j];} // Of for j} // Of for iSystem.out.println("The total scores are:\r\n" + Arrays.toString(totalScore));// Step 3. Find the best and worst student.// Typical initialization for index: invalid value.int tempBestIndex = -1;int temWorstIndex = -1;// Typical initialization for best and worst values.// They must be replaced by valid values.int tempBestScore = 0;int tempWorstScore = m * upperBound + 1;for (int i = 0; i < n; i++) {// Do not consider failed students.if (totalScore[i] == 0) {continue;} // Of ifif (tempBestScore < totalScore[i]) {tempBestScore = totalScore[i];tempBestIndex = i;} // Of if// Attention: This if statement cannot be combined with the last oneif (tempWorstScore > totalScore[i]) {tempWorstScore = totalScore[i];temWorstIndex = i;} // Of if} // Of for i// Step 4. Output the student number and score.if (tempBestIndex == -1) {System.out.println("Cannot find the best student. All studens have failed.");} else {System.out.println("The best student is No." + tempBestIndex + " with scores: "+ Arrays.toString(data[tempBestIndex]));} // Of ifif (temWorstIndex == -1) {System.out.println("Cannot find the worst student. All studens have succeeded.");} else {System.out.println("The worst student is No." + temWorstIndex + " with scores: "+ Arrays.toString(data[temWorstIndex]));} // Of if}// Of task1}// Of class Task1

运行结果:

总结:如果要是我在平时写代码的时候,我会将Step 2.和Step 3.的代码揉到一块儿写,在计算每个同学的总分的时候并找到最大值和最小值及其索引,这样会少一次遍历,当然这也会降低代码的可读性。。。

顺序表

面向对象编程

面向过程(POP) 与 面向对象(OOP)

  • 二者都是一种思想,面向对象是相对于面向过程而言的。面向过程,强调的是功能行为,以函数为最小单位,考虑怎么做。面向对象,将功能封装进对象,强调具备了功能的对象,以类/对象为最小单位,考虑谁来做
  • 面向对象更加强调运用人类在日常的思维逻辑中采用的思想方法与原则,如抽象、分类、继承、聚合、多态等。
  • 面向过程:根据业务逻辑从上到下写代码。
  • 面向对象编程:将数据与函数捆绑到一起,进行封装,这样能够更快速的开发程序,减少了重复写代码的过程。

类与对象

  • 类是一个模板,模板里面可以包含多个函数,函数里实现一些功能。
  • 对象是根据模板创建的实例,通过实例对象可以执行类中的函数。
  • 类的抽象:具有相同或相似属性和行为的一系列对象的集合都可以抽象出一个类。

类(Class)由3部分组成:

  • 类的名称:类名
  • 类的属性:一组数据
  • 类的方法:允许对数据进行操作的方法(行为)

Java中的类和对象

顺序表(一)

这里的代码中,提到了构造函数、方法覆盖、final修饰符。
构造函数:当new一个类的时候,所调用的函数,这也解释了为什么构造函数为什么要和类名一致。
final修饰符:

  • 用final修饰的类不能被继承,没有子类;
  • 用final修饰的方法不能被子类的方法覆盖;
  • 用final修饰的变量表示常量,只能被赋一次值;
    说白了,用final修饰的东西不能改~

为什么能直接打印tempFirstList呢?因为 toString 这个方法很特殊,本身是在 Object类中定义的,其返回值类型为String类型,返回类名和它的引用地址,当进行String类与其他类进行连接操作时,会自动调用toString()方法,而我们在SequentialList类中覆盖了该方法,所以会返回对象的字符串形式。

package day05;public class SequentialList {/*** The maximal length of the list. It is a constant.* */public static final int MAX_LENGTH = 10;/*** The actual length not exceeding MAX_LENGTH. Attention: length is not only the* member variable of Sequential list, but also the member variable of different* classes.*/int length;/*** The data stored in an array.*/int[] data;/*** Construct an empty sequential list.*/public SequentialList() {length = 0;data = new int[MAX_LENGTH];}// Of the first constructor/*** Construct a sequential list using an array.* * @param paraArray The given array. Its length should not exceed MAX_LENGTH.*                  For simplicity now we do not check it.*/public SequentialList(int[] paraArray) {data = new int[MAX_LENGTH];length = paraArray.length;// Copy data.for (int i = 0; i < paraArray.length; i++) {data[i] = paraArray[i];} // Of for i}// Of the second constructor/*** Overrides the method claimed in Object, the superclass of any class.*/public String toString() {String resultString = "";if (length == 0) {return "empty";} // Of iffor (int i = 0; i < length - 1; i++) {resultString += data[i] + ", ";} // Of for iresultString += data[length - 1];return resultString;}// Of toString/*** Reset to empty.*/public void reset() {length = 0;}// Of resetpublic static void main(String args[]) {int[] tempArray = { 1, 4, 6, 9 };SequentialList tempFirstList = new SequentialList(tempArray);System.out.println("Initialized, the list is: " + tempFirstList.toString());System.out.println("Again, the list is: " + tempFirstList);tempFirstList.reset();System.out.println("After reset,the list is: " + tempFirstList);}// Of main}// Of the SequentialList

运行效果:

顺序表(二)

主要完成了对顺序表的差值、插入、删除操作。
在对顺序表进行操作时,如果有明确的索引值,需要首先对索引值是否合法进行判断。插入操作首先要从后向前,依次将目标位置的元素后移一位,最后再对目标位置赋值;删除操作则是要从前向后,将元素向前移动一位,覆盖目标位置的值。
贴上与顺序表(一)不同代码的部分:

 /*** Find the index of the given value. If it appears in multiple positions,* simply return the first one.* * @param paraValue The given value.* @return The position. -1 for not found.*/public int indexOf(int paraValue) {int temPosition = -1;for (int i = 0; i < length; i++) {if (data[i] == paraValue) {temPosition = i;break;} // Of if} // Of for ireturn temPosition;}// Of indexOf/*** Insert a value to position. If the list is already full,do nothing.* * @param paraPosition The given position.* @param paraValue    The given value.* @return Success or not.*/public boolean insert(int paraPosition, int paraValue) {if (length == MAX_LENGTH) {System.out.println("List full.");return false;} // Of ifif ((paraPosition < 0) || (paraPosition > length)) {System.out.println("The position " + paraPosition + " is out of bounds.");return false;} // Of if// From tail to head. The last one is moved to a new position. Because length <// MAX_LENGTHfor (int i = length; i > paraPosition; i--) {data[i] = data[i - 1];} // Of for idata[paraPosition] = paraValue;length++;return true;}// Of insert/*** Delete a value at a position.* * @param paraPosition The position* @return Success or not.*/public boolean delete(int paraPosition) {if ((paraPosition < 0) || (paraPosition >= length)) {System.out.println("The position " + paraPosition + " is out of bounds.");return false;} // Of if// From head to tail.for (int i = paraPosition; i < length - 1; i++) {data[i] = data[i + 1];} // Of for ilength--;return true;}// Of delete/*** The entrance of the program.* * @param args Not used now.*/public static void main(String args[]) {int[] tempArray = { 1, 4, 6, 9 };SequentialList tempFirstList = new SequentialList(tempArray);System.out.println("Initialized, the list is: " + tempFirstList.toString());System.out.println("Again, the list is: " + tempFirstList);int tempValue = 4;int temPosition = tempFirstList.indexOf(tempValue);System.out.println("The position of " + tempValue + " is " + temPosition);tempValue = 5;temPosition = tempFirstList.indexOf(tempValue);System.out.println("The position of " + tempValue + " is " + temPosition);tempValue = 2;temPosition = 5;if (tempFirstList.insert(temPosition, tempValue)) {System.out.println("After inserting  " + tempValue + " to position  " + temPosition + ", the list is: "+ tempFirstList);} // Of iftemPosition = 3;if (tempFirstList.delete(temPosition)) {System.out.println("After deleting data at " + temPosition + ", the list is: " + tempFirstList);} // Of iffor (int i = 0; i < 8; i++) {if (tempFirstList.insert(i, i)) {System.out.println("After inserting " + i + " to position " + i + ", the list is: " + tempFirstList);} // Of if} // Of for itempFirstList.reset();System.out.println("After reset,the list is: " + tempFirstList);}// Of main

运行效果:

总结:对顺序表的操作还算是简单,主要是要注意顺序表的索引和长度的关系以及删除和插入操作的小细节。自己下来还需要多补Java关于类与对象这块的知识~

Day05——综合任务1、顺序表相关推荐

  1. C/C++【顺序表】【初始化、赋值、打印、取值、查找、插入、删除、销毁、综合举例】

    目录 1.运行截图 2.代码 1.运行截图 2.代码 #include <stdio.h> #include <stdlib.h>#define MAXSIZE 100 #de ...

  2. rsa算法c语言实现_数据结构与算法之线性表-顺序表实现(C语言版本)

    原文托管在Github: https://github.com/shellhub/blog/issues/52 数据结构与算法之线性表-顺序表实现(C语言版本) 前言 数据结构与算法是一个程序员必备的 ...

  3. 线性表之顺序表与单链表的区别及优缺点

    这里比较的是基于C语言实现的顺序表与单链表,与其他语言的实现可能会有差异,但我相信语言是相通的,它们的实现机制应该也差不多. 1.What 什么是顺序表和单链表 ①顺序表: 顺序表是在计算机内存中以数 ...

  4. 线性表:4.结合顺序表和链表——静态链表及C语言实现

    另外一种链式表示--静态链表,之前两篇说的都是动态表. 逻辑结构上相邻的数据元素,存储在指定的一块内存空间中,数据元素只允许在这块内存空间中随机存放,这样的存储结构生成的链表称为静态链表. 静态链表和 ...

  5. Java数据结构(1.1):数据结构入门+线性表、算法时间复杂度与空间复杂度、线性表、顺序表、单双链表实现、Java线性表、栈、队列、Java栈与队列。

    数据结构与算法入门 问题1:为什么要学习数据结构          如果说学习语文的最终目的是写小说的话,那么能不能在识字.组词.造句后就直接写小说了,肯定是不行的, 中间还有一个必经的阶段:就是写作 ...

  6. 数据结构翻转课堂答疑实录——顺序表

    [说明] 本文是<数据结构>翻转课堂在线答疑的实录,由云班课的"答疑/讨论"功能中导出数据整理而成.. [重要提示] 下面的内容,按时间从后往前的顺序提供,请直接到文章 ...

  7. 顺序表中删除最小值元素

    2019.6.19 数据结构2.2.3 综合应用题(1) 题目: 从顺序表中删除具有最小值的元素(假设唯一)并由函数返回被删元素的值,空出的位置由最后一个元素填补,若顺序表为空则显示出错信息并退出运行 ...

  8. 数据结构 | 顺序表、链表和数组是逻辑结构还是物理(存储)结构?

    1.逻辑结构和物理结构的定义 首先数据结构分为两个层次:逻辑结构 和 物理结构(存储方式) . 逻辑结构是用来描述数据元素之间的逻辑关系,是一个抽象概念,与数据的实际存储无关,独立于计算机存在. 物理 ...

  9. 【数据结构】顺序表的增删查改 (C语言实现)

    文章目录 一.线性表 二.顺序表 1.什么是顺序表 2.顺序表的分类 三.动态顺序表的实现 1.结构的定义 2.顺序表的初始化 3.检查容量 4.在头部插入数据 5.在尾部插入数据 6.在指定位置插入 ...

  10. 数据结构(07)— 线性顺序表基本操作

    1. 顺序表结构 #define MaxLen 50typedef struct {int data[MaxLen]; // 定义一个数组来存储线性表中所有元素int len; // 存储线性表的长度 ...

最新文章

  1. Scrapyd部署Scrapy框架项目
  2. FT报源检测到目标无法恢复解决过程
  3. 怎么解决tomcat占用8080端口问题图文教程 端口被占用
  4. 百度地图 IOS版开发经验分享
  5. powerdesigner基本使用 - 创建概念模型
  6. SpringBoot+Vue+Itext实现前端请求文件流的方式下载PDF
  7. 使用async,await关键字进行API Access Token的获取
  8. 华为耳机5根线怎么接线图解_联通宽带突然无法上网怎么急救处理?
  9. 乘风破浪的前端小姐姐,是如何一步步走向成功的?
  10. 【转】iPython入门技巧
  11. Nginx 性能调优
  12. iOS 应用状态详解
  13. 将输入的字符串逆序输出
  14. 【PyCharm】PyCharm破解版在系统崩溃后无法启动
  15. c语言vc怎么改变背景板颜色,VC OnCtlColor函数来修改控件背景颜色
  16. 详解C语言中的#define、#undef、#indef、#ifndef、#else、#endif,#if,#elif
  17. 磁盘管理之 raid 文件系统 分区
  18. 在浏览器输入url回车后发生了什么
  19. 像“毒液”一样的粘液机器人火了,能取出体内异物,穿过1.5mm细缝无压力!...
  20. vue项目 el-input输入框字符限制,只显示英文及数字

热门文章

  1. 计算机网络——链路层知识总结
  2. PS打造唯美冷色调照片
  3. 我国三大常用坐标系:北京54、西安80和WGS-84
  4. matlab景深合成算法,关于景深合成,你想知道的都在这儿!
  5. 马陆计算机专业学校,“大路考”有变!智能化监考系统首次投入应用[图]
  6. scipy 概率 泊松分布_概率论中常见分布总结以及python的scipy库使用
  7. 【论文复现】ARBITRAR : User-Guided API Misuse Detectionl
  8. 牛顿的广义二项式定理---微积分推倒的开始
  9. iOS IPv6测试环境搭建及服务器ipv6测试
  10. CentOS Mac 安装zsh,使用oh-my-zsh