《JAVA程序设计》实验报告_第1页
《JAVA程序设计》实验报告_第2页
《JAVA程序设计》实验报告_第3页
《JAVA程序设计》实验报告_第4页
《JAVA程序设计》实验报告_第5页
已阅读5页,还剩52页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、JAVA程序设计实验报告1姓名学号日期实验题目输出自己姓名年龄身高体重,通过控制台和Eclipse运行程序初稿package schooltext;public class MyselfPrint public static void main(String args) System.out.println("ÐÕÃû£ºÑîÊËÇÙ£¬ ÄêÁ䣺18£¬

2、1;í¸ß£º160cm£¬ÌåÖØ£º100½ï ");所遇问题及解决办法调试后的程序代码package schooltext;public class MyselfPrint public static void main(String args) System.out.println("ÐÕÃû£ºÑîÊËÇ&#

3、217;£¬ ÄêÁ䣺18£¬Éí¸ß£º160cm£¬ÌåÖØ£º100½ï ");输出结果ÐÕÃû£ºÑîÊËÇÙ£¬ ÄêÁä£&

4、#186;18£¬Éí¸ß£º160cm£¬ÌåÖØ£º100½ï JAVA程序设计实验报告2姓名杨仕琴学号54121022日期10.13实验题目打印水仙花数程序初稿public class test1 public static void main(String args) for (int j1 = 0; j1 < 10; j1+) for (int j2 = 0; j2 < 10; j2+) for

5、 (int j3 = 0; j3 < 10; j3+) int i=(j1+j2*10+j3*100);int o=(j1*j1*j1+j2*j2*j2+j3*j3*j3);if (i=o) System.out.println(i);所遇问题及解决办法第一次输出结果:03701371153407调试后的程序代码public static void main(String args) for (int j1 = 0; j1 < 10; j1+) for (int j2 = 0; j2 < 10; j2+) for (int j3 = 1; j3 < 10; j3+) i

6、nt i=(j1+j2*10+j3*100);int o=(j1*j1*j1+j2*j2*j2+j3*j3*j3);if (i=o) System.out.println(i);输出结果370371153407JAVA程序设计实验报告3姓名杨仕琴学号54121022日期10.13实验题目编写三个类,按照命名空间规则分别放到不同包中,在代码中调用其它包中的类,并查看类文件在系统中的组织方式程序初稿package unit3_1;public class Fish public Fish()System.out.println("i am fish from package 1"

7、;);package unit3_2;public class Bird public Bird()System.out.println("i am bird from package 2");package unit3_3;public class Pig public Pig()System.out.println("i am pig from package 3");package unit3_3;import unit3_1.Fish;import unit3_2.Bird;public class Unit3Demo public static

8、 void main(String args) Fish f = new Fish();Bird b = new Bird();Pig p = new Pig();System.out.println(f.getClass().getResource("/").getPath();System.out.println(b.getClass().getResource("/").getPath();System.out.println(p.getClass().getResource("/").getPath();所遇问题及解决办法调试

9、后的程序代码输出结果i am fish from package 1i am bird from package 2i am pig from package 3/C:/Users/Administrator/Desktop/jsoft/jd1210.yangshi/bin/C:/Users/Administrator/Desktop/jsoft/jd1210.yangshi/bin/C:/Users/Administrator/Desktop/jsoft/jd1210.yangshi/bin/JAVA程序设计实验报告4姓名杨仕琴学号54121022日期10.13实验题目编写三个类Comput

10、erTime,Plane,Car007和一个接口程序Common.要求在未来如果增加第三种交通工具,不必修改之前任何程序,只要编写新的交通工具代码.从命令行输入ComputerTime的四个参数,第一个是交通工具的类型,二到三是整数A,B,C.程序初稿package unit4;public abstract class Common2 implements Common double A = 0;double B = 0;double C = 0;public void setA(double a) A = a;public void setB(double b) B = b;public

11、void setC(double c) C = c;public abstract void TestV();package unit4;public class Plane2 extends Common2 public void TestV() double v = A + B + C;System.out.println("PlaneµÄËÙ¶ÈÊÇ£º" + v);package unit4;public class Car0072 extends Common2 p

12、ublic void TestV() double v;v = A * B / C;System.out.println("car007µÄËÙ¶ÈÊÇ£º" + v);package unit4;public class ComputerTime2 public static void main(String args) 所遇问题及解决办法主函数传参问题,询问同学和网友获得解答调试后的程序代码package unit4;public interface Common pub

13、lic void TestV();package unit4;import java.util.Scanner;public class Car007 implements Common double A = 0;double B = 0;double C = 0;public void setA(double a) A = a;public void setB(double b) B = b;public void setC(double c) C = c;public void TestV() double v;v = A * B / C;System.out.println("

14、car007µÄËÙ¶ÈÊÇ£º" + v);package unit4;import java.util.Set;public class Plane implements Common double A = 0;double B = 0;double C = 0;public void setA(double a) A = a;public void setB(double b) B = b;public void setC(double c) C = c;public void

15、TestV() double v = A + B + C;System.out.println("PlaneµÄËÙ¶ÈÊÇ£º" + v);package unit4;public class ComputerTime public static void main(String args) throws InstantiationException,IllegalAccessException, ClassNotFoundException Plane com = (Pl

16、ane) Class.forName("unit4.Plane").newInstance();double A = Double.parseDouble(args1);double B = Double.parseDouble(args2);double C = Double.parseDouble(args3);com.setA(A);com.setB(B);com.setC(C);com.TestV();输出结果JAVA程序设计实验报告5姓名杨仕琴学号54121022日期10.13实验题目编写Calculater.java,实现一个简单计算器,要求布局,可以完成加减乘

17、除。程序初稿package unit5;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;import java.math.*;import java.util.*;/* * <p>Title: 用户应用界面计算器</p> * <p>Description: 包含简易计算与功能更加强大的科学计算</p> * <p>Copyright: Copyright (c) 2008</p> * <p>Compan

18、y: 四学</p> * author chwencong * version 1.0 */ public class Calculat static JTextField show;/设置输入区,显示区 static double opNum = 0;/存放操作数 static String opChar = "="/存放操作符 static boolean flag = true;/设置标志 public static void main(String args) new EasyFrame();/产生首界面简易计算器 所遇问题及解决办法流布局方面。解决办法:

19、看书加上问老师调试后的程序代码package unit5;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Calculater extends JFrame implements ActionListener private JPanel jPanel1,jPanel2; private JTextField resultField; private JButton s1,s2,s3,s4,s5,s6,s7,s8,s9,s0,b1,b2,b3,b4,f1,f2; private boolea

20、n end,add,sub,mul,div; private String str; private double num1,num2; public Calculater() super("Calculater"); setSize(300,240); Container con=getContentPane(); con.setLayout(new BorderLayout(); jPanel1=new JPanel(); jPanel1.setLayout(new GridLayout(1,1); jPanel2=new JPanel(); jPanel2.setLa

21、yout(new GridLayout(4,4); resultField=new JTextField("0"); jPanel1.add(resultField); con.add(jPanel1,BorderLayout.NORTH); s1=new JButton(" 1 "); s1.addActionListener(this); s2=new JButton(" 2 "); s2.addActionListener(this); s3=new JButton(" 3 "); s3.addActionL

22、istener(this); s4=new JButton(" 4 "); s4.addActionListener(this); s5=new JButton(" 5 "); s5.addActionListener(this); s6=new JButton(" 6 "); s6.addActionListener(this); s7=new JButton(" 7 "); s7.addActionListener(this); s8=new JButton(" 8 "); s8.addAc

23、tionListener(this); s9=new JButton(" 9 "); s9.addActionListener(this); s0=new JButton(" 0 "); s0.addActionListener(this); b1=new JButton(" + "); b1.addActionListener(this); b2=new JButton(" - "); b2.addActionListener(this); b3=new JButton(" * "); b3.

24、addActionListener(this); b4=new JButton(" / "); b4.addActionListener(this); f1=new JButton(" . "); f1.addActionListener(this); f2=new JButton(" = "); f2.addActionListener(this); jPanel2.add(s1); jPanel2.add(s2); jPanel2.add(s3); jPanel2.add(b1); jPanel2.add(s4); jPanel2

25、.add(s5); jPanel2.add(s6); jPanel2.add(b2); jPanel2.add(s7); jPanel2.add(s8); jPanel2.add(s9); jPanel2.add(b3); jPanel2.add(s0); jPanel2.add(f1); jPanel2.add(f2); jPanel2.add(b4); con.add(jPanel2,BorderLayout.CENTER); public void num(int i) String s = null; s=String.valueOf(i); if(end) /如果数字输入结束,则将文

26、本框置零,重新输入 resultField.setText("0"); end=false; if(resultField.getText().equals("0") /如果文本框的内容为零,则覆盖文本框的内容 resultField.setText(s); else /如果文本框的内容不为零,则在内容后面添加数字 str = resultField.getText() + s; resultField.setText(str); public void actionPerformed(ActionEvent e) /数字事件 if(e.getSourc

27、e()=s1) num(1); else if(e.getSource()=s2) num(2); else if(e.getSource()=s3) num(3); else if(e.getSource()=s4) num(4); else if(e.getSource()=s5) num(5); else if(e.getSource()=s6) num(6); else if(e.getSource()=s7) num(7); else if(e.getSource()=s8) num(8); else if(e.getSource()=s9) num(9); else if(e.ge

28、tSource()=s0) num(0); /符号事件 else if(e.getSource()=b1) sign(1); else if(e.getSource()=b2) sign(2); else if(e.getSource()=b3) sign(3); else if(e.getSource()=b4) sign(4); /等号 else if(e.getSource()=f1) str=resultField.getText(); if(str.indexOf(".")<=1) str+="." resultField.setText

29、(str); else if(e.getSource()=f2) num2=Double.parseDouble(resultField.getText();if(add) num1=num1 + num2; else if(sub) num1=num1 - num2; else if(mul) num1=num1 * num2; else if(div) num1=num1 / num2; resultField.setText(String.valueOf(num1); end=true; public void sign(int s) if(s=1) add=true; sub=fals

30、e; mul=false; div=false; else if(s=2) add=false; sub=true; mul=false; div=false; else if(s=3) add=false; sub=false; mul=true; div=false; else if(s=4) add=false; sub=false; mul=false; div=true; num1=Double.parseDouble(resultField.getText(); end=true; public static void main(String args) Calculater th

31、1=new Calculater(); th1.show(); 输出结果JAVA程序设计实验报告6姓名杨仕琴学号54121022日期10.13实验题目输入若干个整数,并求和,直到遇到结束标志999为止。在输入的过程中,自动忽略掉输入的非整数;通过对负数产生异常的方式,程序初稿 程序初稿忘记保留了,基本上实现了要求但是忽略了自动忽略非整数这一点。对负数的自定义异常也有点问题。所遇问题及解决办法看书解决的自定义异常调试后的程序代码package unit6;import java.util.InputMismatchException;import java.util.Scanner;public

32、 class Sumget private static int i;private static int sum;public static void goOn() throws MyException while (sum >= 0) try Scanner sc = new Scanner(System.in);i = sc.nextInt(); catch (InputMismatchException e) i = 0;if (i >= 0) if (i != 999) sum = sum + i;System.out.println(sum); else break;

33、else throw new MyException(i, sum);public static class MyException extends Exception public MyException(int i1, int sum1) throws MyException i1 = 0;i = i1;sum = sum1;goOn();public static void main(String args) Sumget sumget = new Sumget(); try sumget.goOn(); catch (MyException e) e.printStackTrace()

34、;输出结果JAVA程序设计实验报告7姓名杨仕琴学号54121022日期10.13实验题目程序初稿package unit7;public class Book private String name;private double singlePrice;public String getName() return name;public double getSinglePrice() return singlePrice;public void setName(String name) = name;public void setSinglePrice(double sin

35、glePrice) this.singlePrice = singlePrice;package unit7;public class Books Book book;private int num;public Book getBook() return book;public int getNum() return num;public void setBook(Book book) this.book = book;public void setNum(int num) this.num = num;package unit7;import java.util.HashMap;impor

36、t java.util.Iterator;import java.util.Map;import java.util.Scanner;public class ShopCart private Map book3 = new HashMap(); /每个购物车一个单独的HashMap放东西int MaxOrder = 2;int order = 1;public Map getBook3() / 返回一个购物车中的HashMapreturn book3;public void addBooks() / 向购物车中的HashMap添加Books对象,数目不大于MaxOrderwhile (ord

37、er <= MaxOrder) System.out.println("请输入书名回车结束");Scanner sc1 = new Scanner(System.in);String str = sc1.next();System.out.println("请输入该书单价回车结束");Scanner sc2 = new Scanner(System.in);double dou = sc2.nextDouble();System.out.println("请输入该书购买数目回车结束");Scanner sc3 = new Sca

38、nner(System.in);int i = sc3.nextInt();Books book1 = new Books();Book book2 = new Book();book2.setName(str);book2.setSinglePrice(dou);book1.setBook(book2);book1.setNum(i);book3.put(order, book1);order+;public void getSum(HashMap bookss) /取得购物车HashMap中的信息算出总价格double sum = 0.0;Books book5 = new Books()

39、;for (Iterator iterator = bookss.keySet().iterator(); iterator.hasNext();) book5 = (Books)iterator.next();sum += (book5.getNum() * (book5.getBook().getSinglePrice();System.out.println(sum);package unit7;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Scanner;

40、public class ShopCartDemo public static void main(String args) ShopCart shop=new ShopCart();/新建一个购物车shop.addBooks(); /购物车中加东西HashMap book4=(HashMap)shop.getBook3(); shop.getSum(book4); /购物车算总价所遇问题及解决办法While 条件写错,找了好久。调试后的程序代码package unit7;public class Book private String name;private double singlePr

41、ice;public String getName() return name;public double getSinglePrice() return singlePrice;public void setName(String name) = name;public void setSinglePrice(double singlePrice) this.singlePrice = singlePrice;package unit7;public class Books Book book;private int num;public Book getBook() r

42、eturn book;public int getNum() return num;public void setBook(Book book) this.book = book;public void setNum(int num) this.num = num;package unit7;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Scanner;public class ShopCart private Map book3 = new HashMap();

43、 /每个购物车一个单独的HashMap放东西int MaxOrder = 2;int order = 1;public Map getBook3() / 返回一个购物车中的HashMapreturn book3;public void addBooks() / 向购物车中的HashMap添加Books对象,数目不大于MaxOrderwhile (order <= MaxOrder) System.out.println("请输入书名回车结束");Scanner sc1 = new Scanner(System.in);String str = sc1.next();S

44、ystem.out.println("请输入该书单价回车结束");Scanner sc2 = new Scanner(System.in);double dou = sc2.nextDouble();System.out.println("请输入该书购买数目回车结束");Scanner sc3 = new Scanner(System.in);int i = sc3.nextInt();Books book1 = new Books();Book book2 = new Book();book2.setName(str);book2.setSingleP

45、rice(dou);book1.setBook(book2);book1.setNum(i);book3.put(order, book1);order+;public void getSum(HashMap bookss) /取得购物车HashMap中的信息算出总价格double sum = 0.0;Books book5 = new Books();for (Iterator iterator = bookss.keySet().iterator(); iterator.hasNext();) book5 = (Books)iterator.next();sum += (book5.get

46、Num() * (book5.getBook().getSinglePrice();System.out.println(sum);package unit7;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Scanner;public class ShopCartDemo public static void main(String args) ShopCart shop=new ShopCart();/新建一个购物车shop.addBooks(); /购物车中加

47、东西HashMap book4=(HashMap)shop.getBook3(); shop.getSum(book4); /购物车算总价输出结果JAVA程序设计实验报告8姓名杨仕琴学号54121022日期10.13实验题目程序初稿package unit8;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;public class ddd extends JFrame implements ActionListener JTextArea txtMessage;JButton btn

48、Read, btnWrite;public ddd() super("文本读写");txtMessage = new JTextArea(15, 30);btnRead = new JButton("读取");btnWrite = new JButton("写入");this.setLayout(new FlowLayout();this.add(new JLabel("文件内容:");this.add(txtMessage);this.add(btnRead);this.add(btnWrite);btnRead

49、.addActionListener(this);btnWrite.addActionListener(this);this.setSize(400, 400);this.setVisible(true);this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);public static void main(String args) new ddd();public void actionPerformed(ActionEvent e) JButton btn = (JButton)e.getSource();String fileName

50、 = "D:test.txt"if(btn.getText().equals("读取")tryInputStream is = new FileInputStream(fileName);BufferedReader br = new BufferedReader(new InputStreamReader(is);String line = null;StringBuffer buffer = new StringBuffer();while(line = br.readLine() != null)buffer.append(line + "

51、;n");txtMessage.setText(buffer.toString();br.close();is.close();else if(btn.getText().equals("写入")tryString msg = txtMessage.getText();OutputStream os = new FileOutputStream(fileName);PrintStream ps = new PrintStream(os);ps.print(msg);ps.close();os.close(); 所遇问题及解决办法文件读写的类太多,查API查很久调试

52、后的程序代码package unit8;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;public class ddd extends JFrame implements ActionListener JTextArea txtMessage;JButton btnRead, btnWrite;public ddd() super("文本读写");txtMessage = new JTextArea(15, 30);btnRead = new JButton("读取");btnWrite = new JButton("写入");this.setLayout(new FlowLayout();this.add(new JLabel("文件内容:");this.add(txtMessage);this.add(btnRead);this.add(btnWrite);btnRead.addActionListener(this);btnWrit

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论