没有合适的资源?快使用搜索试试~ 我知道了~
首页河北工业大学JAVA实验报告
河北工业大学JAVA实验报告
需积分: 40 20 下载量 155 浏览量
更新于2023-03-16
评论
收藏 1.71MB DOCX 举报
河北工业大学JAVA实验报告,我觉得这个实验最有意思的还是聊天室,灵活运用TCP协议与UDP协议
资源详情
资源评论
资源推荐
实验一 Java 语言基础
一、实验目的:
掌握 Java 语言的基本语法、基本数据类型的使用方法,掌握从键盘输入基本类型的数
据,熟练运用分支、循环等语句控制程序流程。
二、实验内容:
1、编写一个 Java 应用程序,用户从键盘输入十名学生的信息,至少包括姓名、年龄、出
生年月日、java 课程实验成绩,成绩使用浮点数,年龄使用整型,程序将输出年龄、java
课程实验成绩的平均值。
提示:Scanner 对象调用 nextDouble()或 nextFloat()可以获取用户从键盘输入的浮点数。
2、使用 Arrays 类实现数组排序:使用 java.util 包中的 Arrays 类的类方法 public static void
sort(double a[])可以把参数 a 指定的 double 类型数组按升序排序;public static void
sort(double a[], int start , int end)可以把参数 a 指定的 double 类型数组中从位置 start 到 end 位
置的值按升序排序。
给定数组 int a[]={12,34,9,-23,45,6,90,123,19,45,34}; 从键盘读入一个整数,使用折半查找判
断该整数是否在这个数组中,并将结果输出。
3、输出 100~200 之间的所有素数。
4、采用 for 循环求 1 至 1000 之内的所有“完全数”。所谓“完全数”是指一个数,恰好等于它
的因子之和。例如,6 是一个完全数,因为 6 的因子为 1、2、3,而 6=1+2+3。
5、已知 XYZ+YZZ=532,其中 X、Y 和 Z 为数字,编程求出 X,Y 和 Z 的值。
三、代码与运行结果
1.
日期:
import java.util.Scanner;
public class myDate {
public static Scanner i=new Scanner(System.in);
protected int year,month,day;
protected static boolean a=false;
public myDate(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public myDate(){
this(0,0,0);
}
public myDate(myDate d){
this(d.year,d.month,d.day);
}
public int getYear(){
this.year=i.nextInt();
return this.year;
}
public int getMonth(){
this.month=i.nextInt();
return this.month;
}
public int getDay(){
this.day=i.nextInt();
return this.day;
}
public int days(int month,int year) //获得具体年份的每月日期信息
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if(year%400 == 0 ||year%100!=0&&year%4==0)
return 29;
else
return 28;
default:
return 0;
}
}
public void input(){
System.out.println("请输入年份");
getYear();
if(year>2017){
System.out.println("输入年份错误!请重新输入年份");
getYear();
}
System.out.println("请输入月份");
getMonth();
if(month<1||month>12)
{
System.out.println("月份输入错误!请重新输入月份");
getMonth();
}
int days=days(month,year);
System.out.println("请输入日期");
getDay();
if(day<1||day>days){
System.out.println("日期输入错误!请重新输入日期");
getDay();
}
}
public String toString()
{
return year+"."+month+"."+day;
}
public void output(){
System.out.println("\t"+year+"."+month+"."+day);
}
}
人:
import java.util.Scanner;
public class Person { //使用 myDate 作为成员变量
protected String name,gender;
protected int age;
protected myDate birthday;
protected static int count=0,thisyear=2017;
protected static Scanner SCAN=new Scanner(System.in);
public Person(String name,String gender,int age,myDate birthday){
this.name=name;
this.gender=gender;
this.age=age;
this.birthday=birthday;
}
public Person(){
}
public Person(Person p){
this(p.name,p.gender,p.age,new myDate(p.birthday)); //实现深拷贝
}
public String getName() {
this.name=SCAN.next();
return name;
}
public String getGender(){
this.gender=SCAN.next();
return gender;
}
public int getAge(){
age=thisyear-this.birthday.year;
return age;
}
public void input(){
System.out.println("请输入姓名");
this.getName();
System.out.println("请输入性别");
this.getGender();
this.birthday=new myDate(); //曾出错:未创建一个 myDate 对象就使用 input
方法
this.birthday.input();
this.getAge();
}
public void output(){
System.out.println(" 姓 名 : "+this.name+", 性 别 : "+this.gender+", 出 生
于:"+this.birthday.toString()+",当今岁数:"+this.age+"岁");
}
}
学生类:
import java.util.Scanner;
public class Student extends Person {
private String number;
private String speciality;
private float grade;
protected static Scanner SCAN=new Scanner(System.in);
public Student (String name,String gender,int age,myDate birthday,String spec,float grade){
super(name,gender,age,birthday); //父类的参数在子类也要声明
this.number="0101"+String.format("%04d",thisyear)+String.format("%04d", count);
this.speciality=spec;
this.grade=grade;
count++; //记录人数
}
public Student(){
count++; //记录人数
}
public Student(Student s){
this(s.name,s.gender,s.age,new myDate(s.birthday),s.speciality,s.grade);
}
public float getGrade(){
this.grade=SCAN.nextFloat();
return this.grade;
}
public String getNumber(){
this.number="0101"+String.format("%04d",thisyear)+String.format("%04d", count);
return this.number;
}
public String getSpeciality(){
speciality=SCAN.next();
return speciality;
}
public void Input(){
this.input();
System.out.println("请输入专业:");
this.getSpeciality();
System.out.println("请输入实验成绩:");
this.getGrade();
this.getNumber();
}
public void Output(){
this.output();
System.out.println("专业:"+this.speciality);
System.out.println("学号:"+this.number);
System.out.println("实验成绩:"+this.grade);
}
public static void main(String[] args){
System.out.println("请输入想要录入的学生人数:");
Scanner s=new Scanner(System.in);
float average=0,total=0;
int n=s.nextInt(); //想要录入的学生人数
Student[] st=new Student[n];
for(int i=0;i<n;i++){
Student t=new Student();
t.Input();
st[i]=t;
total=total+t.grade;
}
for(int i=0;i<n;i++){
st[i].Output();
}
average=total/count;
剩余63页未读,继续阅读
Eccentric_t
- 粉丝: 99
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
- SPC统计方法基础知识.pptx
- MW全能培训汽轮机调节保安系统PPT教学课件.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0