小奥的学习笔记

  • Home
  • Learning & Working
    • Speech Enhancement Notes
    • Programming language
    • Computer & DL
    • MOOC
  • Life
    • Life Time
    • Thinking & Comprehension
    • Volunteer
    • Plan
    • Travel
  • Footprints
  • GuestBook
  • About
    • About Me
    • 个人履历
    • 隐私策略
  1. 首页
  2. Study-notes
  3. Programming language
  4. Java
  5. 正文

Java语言程序设计【学堂在线】编程作业(第三章)

2018年3月13日 1650点热度 0人点赞 0条评论

程序1:教师学生评分

学校要进行年终总结,需要对教师和学生的评分结果进行统计。学生的统计数据有三个,教师的统计数据有四个。请你实现一个统计系统,对输入的数据进行整理。
请你实现一个Person类表示人员,并实现一些必要的方法,再实现Teacher类和Student类,通过类的继承机制完成这个任务。
输入格式:
首先输入一个数字N,表示输入统计的人数。
接下来是N行,每行是用空格隔开的一系列数字。
输出格式:
N行,每行是一个标识符加一个平均得分(向下取整的整数),用空格隔开。
学生的标识符是Student,教师的标识符是Teacher。
输入样例:
2
2 3 4
2 3 4 5
输出样例:
Student 3
Teacher 3

我的程序只得了90分不知道为何,求解。

import java.util.*;  
class Person{
    final String Identity = "persion";
    int[] score;
    public void getScore() {
        for(int m=0;m<score.length;m++) {
             this.score[m] = score[m]; 
        }
    }
    public String getIdentity() {
        return Identity;
    }
    public int average() {
        int sum = 0;
        int avg = 0;
        for(int number = 0; number<this.score.length; number++)
        {
            sum += this.score[number];
        }
        avg = (int)Math.floor(sum/this.score.length);
        return avg;
    }
}
class Student extends Person{
    final String Identity = "Student";
    public String getIdentity() {
        return Identity;
    }
    public int[] setScore(int[] score) {
        return this.score = score;
    }
}
class Teacher extends Person{
    final String Identity = "Teacher";
    public String getIdentity() {
        return Identity;
        }
    public int[] setScore(int[] score) {
        return this.score = score;
    }
}
public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int number = Integer.parseInt(in.nextLine());
        String[] str = new String[number];
        for(int i =0;i<number;i++)
        {
            str[i] = in.nextLine();
        }
        for(int n=0;n<number;n++) {
            String[] temp = str[n].split(" ");
            
            if(temp.length == 3) {
                int[] score = new int[3];
                for(int m=0; m<3; m++) {
                    score[m] = Integer.parseInt(temp[m]);
                }
                Student student =new Student();
                student.setScore(score);
                System.out.println(student.getIdentity() + 
                " " + student.average());
            }
            if(temp.length == 4) {
                int[] score =new int[4];
                for(int m = 0; m<4; m++) {
                    score[m] = Integer.parseInt(temp[m]);
                }
                Teacher teacher = new Teacher();
                teacher.setScore(score);
                System.out.println(teacher.getIdentity() +
                 " " + teacher.average());
            }
        }
    }
}

程序作业2:图形面积计算

我们有一些图形的边长数据,这些图形包括三角新和矩形,请你编写一个程序求出它们的面积。
请你实现一个基础图形类Graph,然后实现三角形类Triangle和矩形类Rectangle,继承自Graph。根据输入的边数实现不同的对象,并计算面积。
输入格式:
一行,一个整数n,表示图形个数。
n行,每行是用空格隔开的整数。
输出格式:
n行,每行是一个图形的面积。
输入样例:
2
5 5
6 6 6
输出样例:
25
15

import java.util.*;//导入库
class Graph{
    int[] bc;
}
class Triangle extends Graph{
    public int[] setBc(int[] bc) {
        return this.bc = bc;
    }
    public int acreage() {
        int acre = 0;
        double p = (bc[0]+bc[1]+bc[2])/2.0;;
        return acre = (int) Math.sqrt(p*(p-bc[0])*(p-bc[1])*(p-bc[2]));
    }
    public boolean triangletorf(int i1, int i2, int i3) {
        if(i1+i2<i3||i1+i3<i2||i2+i3<i1)
        {
            return false;
        }
        else
            return true;
    }
    
    
}
class Rectangle extends Graph{
    public int[] setBc(int[] bc) {
        return this.bc = bc;
    }
    public int acreage() {
        int acre = 0;
        return acre =(int) Math.floor(bc[0]*bc[1]);
    }
    
}
public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        int number = Integer.parseInt(input.nextLine());;
        String[] str = new String[number];
        for(int i =0; i<number;i++) {
            str[i] = input.nextLine();
        }
        for(int j=0;j<number;j++) {
            String[] temp = str[j].split(" ");//以空格为标志对每一行的字符串进行分割,分割成子字符串
            //开始判断图像,首先是矩形
            if(temp.length ==2) {
                int[] bc = new int[2];
                for(int m=0; m<2; m++) {
                    bc[m] = Integer.parseInt(temp[m]);//同样的,字符穿转换成int型
                }
                Rectangle rect = new Rectangle();
                //student.setScore(score);
                rect.setBc(bc);
                System.out.println(rect.acreage());
                //设置长度,输出长度,由于相应类函数还没有写,所以暂时不写
                
            }
            if(temp.length == 3) {
                int[] bc = new int[3];
                for(int m=0;m<3;m++) {
                    bc[m] = Integer.parseInt(temp[m]);
                }
                Triangle tri = new Triangle();
                tri.setBc(bc);
                if(tri.triangletorf(bc[0], bc[1], bc[2])) {
                    System.out.println(tri.acreage());
                }
                
                //设置长度,输出长度,由于相应函数还没有写,所以暂时不写
            }
        }
        
    }
}

程序作业3:多类型排序

我们现在有一些数据,是整数和字符串混杂的。现在需要你将他们分开,并且分别进行排序。
输入格式:
一行,一个数字n,表示元素的格式。
n行,每行一个字符串整数,也可以是其他字符串。
输出格式:
n行,前面一部分为输入的整数字符串按从小到大排序输出,后面一部分为非整数字符串按照字典序从小到大输出。
输入样例:
5
12
ab
bd
32
t
输出样例:
12
23
ab
bd
t

import java.util.*;
/*感谢崔崔写出此程序*/
public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int number = Integer.parseInt(s.nextLine());//将字符串5转化为数字5
        ArrayList str = new ArrayList();
        ArrayList i=new ArrayList();
        int m=0,n=0;
        for(int j =0;j<number;j++)
        {
            //int c1=0,c2=0;
            if(s.hasNextInt()) {
                i.add(s.nextInt());
                m++;
            }
            else {
                String a = s.next();
                str.add(a);
                n++;
            }
        }
        Collections.sort(i);
        for(int l=0;l<i.size();l++) {
            System.out.println(i.get(l));
        }
        Collections.sort(str);
        for(int k=0;k<str.size();k++) {
            System.out.println(str.get(k));
        }
        
    }
}

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: Java
最后更新:2018年3月13日

yszhang

这个人很懒,什么都没留下

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

搜索
欢迎关注我的个人公众号
最新 热点 随机
最新 热点 随机
DEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架 奥地利匈牙利九日游旅程 论文阅读之Study of the General Kalman Filter for Echo Cancellation 小奥看房之鸿荣源珈誉府 杭州往返旅途及西溪喜来登和万怡的体验报告 2022年的第一篇碎碎念
奥地利匈牙利九日游旅程论文阅读之Study of the General Kalman Filter for Echo CancellationDEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架
小花豆生活第∞天:写在青岛世园会闭幕之时 WIN10+anaconda+CUDA9.0+CUDNN7.0安装配置Tensorflow(GPU)教程 Leetcode 14:最长公共前缀 算法笔记之分支限界法(1) 《剑指Offer》题目解析(5) 个人用VB做的一个简单的计算器
标签聚合
高中 鸟哥的linux私房菜 Java Python 算法 leetcode 生活 学习 python学习 linux
最近评论
davidcheung 发布于 7 个月前(02月09日) The problem has been fixed. May I ask if you can s...
tk88 发布于 7 个月前(02月07日) Hmm is anyone else having problems with the pictur...
cuicui 发布于 11 个月前(10月20日) :wink:
niming 发布于 12 个月前(09月19日) 同级校友,能刷到太巧了
davidcheung 发布于 2 年前(08月16日) 我得找一下我之前整理的word文档看一下,如果找到了我就更新一下这篇文章。
Nolan 发布于 2 年前(07月25日) 您的笔记非常有帮助。贴图不显示了,可以更新一下吗?
davidcheung 发布于 3 年前(06月19日) 到没有看webrtc的代码。现在主要在看我们公司的代码了。。。只是偶尔看一看webrtc的东西。。。
aobai 发布于 4 年前(03月13日) gain_change_hangover_ 应该是每三个block 只能够调整一次,这样保证每帧...
匿名 发布于 5 年前(12月30日) 烫
小奥 发布于 5 年前(12月12日) webRTC里面的NS本身我记得就是在C++里面呀

COPYRIGHT © 2025 小奥的学习笔记. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

陕ICP备19003234号-1

鲁公网安备37120202000100号