




已阅读5页,还剩26页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
java编程1.使用记事本编写一个名为Exp1_1.java的JAVA源程序,程序的功能输出个人的真实信息,包括班级、姓名、学号package exp1_1;public class Exp1_1 public static void main(String args) System.out.println(班级:1107班,姓名:徐燕梅,学号:111010215); 2.使用文本编辑软件Editplus创建一个名为Exp1_2.java的JAVA源程序,程序要求输入两个整数,比较这两个整数大小,并输出大的数。package exp1_2;import java.util.Scanner;public class Exp1_2 public static void main(String args) int a,b; Scanner in=new Scanner(System.in); /输入方法 System.out.println(请输入2个整数:); a=in.nextInt(); /输入整数 b=in.nextInt(); if(ab) System.out.println(max=+a); else System.out.println(max=+b); 3. 使用Netbeans编写一个名为Exp1_3的项目,用于输出n行由*号组成的图形,其中第i行共有2i-1个*号。package exp1_3;public class Exp1_3 public static void main(String args) int n=9; for(int i=1;i=n;i+) for(int j=1;j=2*i-1;j+) System.out.print(*); System.out.println(); 4. 使用Netbeans编写一个名为Exp1_4的项目,在该项目的main函数中定义一个int型数组,数组的大小为50,随机产生每个数组元素的值,要求每个数组元素的值均在0至100之间,求出该数组的平均值,输出其平均值和最大值。package exp1_4;public class Exp1_4 private static int i; public static void main(String args) int num=new int50; int sum=0; int max=0; for( i=0;i50;i+)或for(int i=0;imax) max=numi; System.out.println(平均值为+sum/50.0); System.out.println(max=+max);5. 使用Netbeans编写一个名为Exp1_5的项目用于输出乘法表package exp1_5;public class Exp1_5 public static void main(String args) for(int i=1;i=9;i+) for(int j=1;jj) a=-a*x*x/(2*i+1)*(2*i+2); sum=sum+a; i+; System.out.println(cos+x+的值为:+sum); public static void main(String args) System.out.print(输入x的值:); Scanner input=new Scanner(System.in); double a=input.nextDouble(); Exp2_1 b=new Exp2_1(); b.calcos(a); 2. 编写一个名为Exp2_2的java程序,用于输出21世纪某年某月的月历,其中年、月在运行时输入。package exp2_2;import java.util.Scanner;public class Exp2_2 public static void main(String args) int day=0,0,31,59,90,120,151,181,212,243,273,304,334,365; Scanner in=new Scanner(System.in); boolean bool=true; int m=1; while(bool) System.out.print(请输入月份(112):); m=in.nextInt(); if(m=1&m=12) bool=false; else System.out.println(月份必须在112:); int start=2; System.out.println(日 一 二 三 四 五 六); int i,n; n=(start+daym)%7; for(i=0;i=daym+1-daym;i+) if(n%7=0) System.out.println(); System.out.printf(%3d, i); n+; System.out.println(); 3. 编写一个名为Exp2_3的java程序,从键盘输入一行文本,分别输出每个单词以及单词的数目。假设文本行只有字母和空格,每个单词由若干个字母组成,单词之间由一个或多个空格分隔。要求输出时每行显示一个单词。package exp2_3;import java.util.Scanner;public class Exp2_3 public static void main(String args) String str; Scanner in=new Scanner(System.in); System.out.println(请输入一串文本); str=in.nextLine(); System.out.println(str); int count=0; boolean isSpace=true; for(int i=0;istr.length();i+) if(str.charAt(i)!= ) System.out.print(str.charAt(i); isSpace=true; else if(isSpace) isSpace=false; System.out.println(); count+; if(str.charAt(str.length()-1)!= ) count+; System.out.println(共有+count+单词n); 1.在该项目中新建一个java类:Box类,包括private成员变量length、width、heigth,public成员方法:设置与返回各成员变量的方法;构造方法。在主类的main方法中创建该类的对象,并输出其体积。主类:package exp3;public class Exp3 public static void main(String args) Box b=new Box(2,2,2); int a=b.Value(); System.out.println(体积a=+b.Value(); Box类:package exp3;public class Box private int length,width,heigth; public Box() length=2; width=2; heigth=2; public int Value() return length*width*heigth; public Box(int length, int width, int heigth) this.length = length; this.width = width; this.heigth = heigth; 2.创建一个圆类Circle,在主类的主方法中创建该类对象,并输出圆的面积.package circle;public class Circle int r; public static void main(String args) Circle cirl=new Circle(2); System.out.println(cirl.r*cirl.r*3.14); public Circle(int r) this.r = r; 1.创建一个名为Exp4_1的项目。在该项目的exp4_1包中新建一个日期类Date,在Exp4_1中创建一个包:student,在该包中创建一个学生类:Stu,一个成绩类Score。Exp4_1类:package exp4_1;import Student.*; public class Exp4_1 public static void main(String args) Date data=new Date(1998,1,22); System.out.println(data); Score score=new Score(87,99,88); System.out.println(score); Stu stu=new Stu(001,小小); stu.setBirthday(2121,1,1); stu.setScore(77,88,99); stu.printstu(); Date类:package exp4_1;public class Date int year, month, day; public Date(int year, int month, int day) this.year = year; this.month = month; this.day = day; public Date() public int getDay() return day; public void setDay(int day) this.day = day; public int getMonth() return month; public void setMonth(int month) this.month = month; public int getYear() return year; public void setYear(int year) this.year = year; Override public String toString() return year + 年 + month + 月 + day + 日; Stu类:package Student;import exp4_1.*;public class Stu String no; String name; Date birthday; Score score; public Stu(String no, String name) this.no = no; = name; birthday=new Date(); score=new Score(); public String getName() return name; public void setBirthday(int y ,int m,int d) this.birthday.setYear(y); this.birthday.setMonth(m); this.birthday.setDay(d); public void setScore(int c,int m,int e) this.score.chinese=c; this.score .math=m; this.score.english=e; this.score.setChinese(e); this.score.setMath(m); this.score.setEnglish(e); public void setName(String name) = name; public String getNo() return no; public void setNo(String no) this.no = no; public void printstu() System.out.println(学号:+no); System.out.println(姓名:+name); System.out.println(出生日期:+this.birthday); System.out.println(成绩:+this.score); Score类: package Student;public class Score int chinese,math,english; public Score(int chinese, int math, int english) this.chinese = chinese; this.math = math; this.english = english; public Score() public int getChinese() return chinese; public int getMath() return math; public int getEnglish() return english; public void setChinese(int chinese) this.chinese = chinese; public void setMath(int math) this.math = math; public void setEnglish(int english) this.english = english; Override public String toString() return 语文:+chinese+,数学:+math+,英语:+english; 2.将课程的编码、课程名称、考试日期、教师姓名 定义为类变量,并添加一个类方法用于设置这些类变量的值,添加一个类方法用于输出这些类变量的值,在主方法中通过类名.类方法名设置课程信息和输出,而学生信息通过构造方法设置。Exp5_2类:package exp5_2;public class Exp5_2 public static void main(String args) ScoreReport stu=new ScoreReport2; ScoreReport.setCourse(001,数分,2013.6.25,李教授); stu0=new ScoreReport(101001,小李,78); stu1=new ScoreReport(101002,小陈,88); ScoreReport.printCourse(); System.out.println(stu0); System.out.println(stu1); ScoreReport类:package exp5_2;public class ScoreReport static String courseID; static String courseName; static String testDate; static String teacherName; String stuID; String strName; int score;public static void setCourse(String ID,String Name,String Date,String teacher) courseID=ID; courseName=Name; testDate=Date; teacherName=teacher; public ScoreReport(String stuID, String strName, int score) this.stuID = stuID; this.strName = strName; this.score = score; public static String getCourseID() return courseID; public static String getCourseName() return courseName; public int getScore() return score; public void setScore(int score) this.score = score; public String getStrName(String strName) return strName; public void setStrName(String strName) this.strName = strName; public String getStuID() return stuID; public void setStuID(String stuID) this.stuID = stuID; public static String getTeacherName() return teacherName; public static String getTestDate() return testDate; public static void printCourse() System.out.println(课程编号:+courseID+课程名称:+courseName); System.out.println(授课教师:+teacherName+考试日期:+testDate); Override public String toString() return 学号: + stuID + , 姓名: + strName + ,成绩: + score + ; 1. 几何图元类层次的设计父类:几何图元类Shape子类1:矩形类Rectangle(父类为几何图元类)子类2:正方形类Square(父类为矩形类)Exp6_1类:package exp6_1;import java.awt.Color;import java.awt.Point;import java.awt.Rectangle;public class Exp6_1 public static void main(String args) Point p=new Point(300,300); Color c=new Color(255,255,0); Rectangle rect=new Rectangle(100,200); System.out.println(rect); Shape类: package exp6_1;import java.awt.Color;import java.awt.Point;public class Shape Color color; Point point; public Shape() color = new Color(); point = new Point(0, 0); public Shape(Color c, Point p) this.color = c; this.point = p; color = new Color(); point = new Point(p); Override public String toString() return Shape + 颜色: + color + , 点: + point + ; class Color int r, g, b; public Color() r = 0; g = 0; b = 0; public Color(int r, int g, int b) this.r = r; this.g = g; this.b = b; Override public String toString() return 颜色 + r= + r + , g= + g + , b= + b + ; /矩形类 class Rectangle extends Shape int length, width; public Rectangle() public Rectangle(int length, int width, Color c, Point p) super(c, p); this.length = length; this.width = width; public Rectangle(int length, int width) super(); this.length = length; this.width = width; public int area() return length * width; public int permenter() return 2 * (length + width); Override public String toString() return super toString + length= + length + , width= + width; /正方形类 class Square extends Rectangle public Square(int length) this.length = length; public Square(int length, Color c, Point p) super(length, length, c, p); this.length = length; Override public String toString() return 正方形: + super.toString(); 1.设计一个抽象类动物类:Animal,动物类的子类:Dog类、Cat类、Duck类.Exp7_1类:package exp7_1;public class Exp7_1 public static void main(String args) AnimaltheAnimals= new Dog(小一,狗),new Cat(小二,猫),new Duck(小小,鸭) ; for(int i=0;itheAnimals.length;i+) theAnimalsi.sound(); Animal类:package exp7_1;public abstract class Animal protected String type; protected String name; protected String breed; public Animal(String type, String name, String breed) this.type = type; = name; this.breed = breed; Override public String toString() return Animal + type= + type + , name= + name + , breed= + breed + ; public abstract void sound(); Dog类:package exp7_1;public class Dog extends Animal public Dog(String name) super(Dog,name,Unknow); public Dog(String name,String breed) super(Dog,name,breed); Override public void sound() System.out.println(狗的叫声是汪汪); Cat类:package exp7_1;public class Cat extends Animal public Cat(String name) super(Cat,name,Unknow); public Cat(String name,String breed) super(Cat,name,breed); Override public void sound() System.out.println(猫的叫声是喵喵); Duck类:package exp7_1;public class Duck extends Animal public Duck(String name) super(Duck,name,Unknow); public Duck(String name,String breed) super(Duck,name,breed); Override public void sound() System.out.println(鸭的叫声是嘎嘎叫); 1.在D:A下创建一个文件a.txt,向文件中写入下面字符串:专业:信息与计算科学年级:11级成绩:86.5然后读出文件中内容并输出package exp8;import java.io.*;public class Exp8 public static void main(String args) File dir = new File(d:/a/a.txt);/定义文件对象 try dir.createNewFile();/创建文件 try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(dir) dos.writeChars(专业:信息与计算科学); dos.writeChar(n); dos.writeChars(年级:11级); dos.writeChar(n); dos.writeChars(成绩:86.5); dos.writeChar(n); try (DataInputStream dis = new DataInputStream(new FileInputStream(dir) int n; while(n=dis.readChar()!=-1) System.out.print(char)n); catch (Exception e) 1.设计一个如下图所示的界面package exp9_1;import java.awt.BorderLayout;import javax.swing.*;public class Exp9_1 extends JFrame JPanel jp1=null; JPanel jp2=null; JPanel jp3=null; JLabel label1=null; JTextField textfield=null; JLabel label2=null; JPasswordField password=null; JButton jb1=null; JButton jb2=null; public static void main(String args) Exp9_1 Exp9_1=new Exp9_1(); public Exp9_1() this.setTitle(实验9_1); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocation(200, 200); jp1=new JPanel(); jp2=new JPanel(); jp3=new JPanel(); label1=new JLabel(用户名:); textfield=new JTextField(10); label2=new JLabel(密码:); password=new JPasswordField(10); jb1=new JButton(确定); jb2=new JButton(取消); jp1.add(label1); jp1.add(textfield); this.add(jp1,BorderLayout.NORTH); jp2.add(label2); jp2.add(password); this.add(jp2); jp3.add(jb1); jp3.add(jb2); this.add(jp3,BorderLayout.SOUTH); this.pack(); this.setVisible(true); package exp9_2;import javax.swing.*;import java.awt.*; public class Exp9_2 extends JFrame JPanel jp1=null; JPanel jp2=null; JTextField textfield=null; String str=0,1,2,3,4,5,6,7,8,9,+,-,*,/,=; JButton jb=new JButton15; public static void main(String args) Exp9_2 Java9_2=new Exp9_2(); public Exp9_2() this.setTitle(简易计算器);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocation(400,400); jp1=new JPanel(); jp2=new JPanel(); textfield=new JTextField(10); jp1.add(textfield); this.add(jp1,BorderLayout.NORTH); jp2.setLayou
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 海上光伏产业技术创新:2025年绿色能源产业技术创新与市场趋势报告
- 2025年电商平台内容营销与种草经济品牌影响力研究报告
- 烟草车辆创新管理办法
- 热力车间指标管理办法
- 煤矿应急处理管理办法
- 矿山全面预算管理办法
- 网点服务考核管理办法
- 瓯北代理会计管理办法
- 滁州财政债务管理办法
- 法院电子归档管理办法
- 扣件式钢管脚手架培训课件
- 物业沟通技巧培训
- 2025至2030中国美容祛斑仪行业发展趋势分析与未来投资战略咨询研究报告
- 民宿旅游培训课件
- 2025-2030年中国连续性肾脏替代治疗(CRRT)行业市场现状供需分析及投资评估规划分析研究报告
- 现场员工计件管理制度
- 健康养老课件模板
- DZ/T 0261-2014滑坡崩塌泥石流灾害调查规范(1∶50 000)
- 计算机二级MySQL真题下载试题及答案
- 佛山市顺德区容桂街道专业电镀产业发展规划(2023-2035年)环境影响报告书(简本)
- 高效人员管理的5大核心思路与方法
评论
0/150
提交评论