《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( 18160cm100 );所遇问题及解决办法调试后的程序代码package schooltext;public class MyselfPrint public static void main(String args) System.out.println( 18160cm100 );

2、输出结果 18160cm100 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 (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);所遇

3、问题及解决办法第一次输出结果: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+) 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);输出结果370371153407JAVA程序设计实验报告3姓名杨仕琴学号54121022日期10.

4、13实验题目编写三个类,按照命名空间规则分别放到不同包中,在代码中调用其它包中的类,并查看类文件在系统中的组织方式程序初稿package unit3_1;public class Fish public Fish()System.out.println(i am fish from package 1);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

5、.println(i am pig from package 3);package unit3_3;import unit3_1.Fish;import unit3_2.Bird;public class Unit3Demo public static 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().get

6、Resource(/).getPath();System.out.println(p.getClass().getResource(/).getPath();所遇问题及解决办法调试后的程序代码输出结果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/Adminis

7、trator/Desktop/jsoft/jd1210.yangshi/bin/JAVA程序设计实验报告4姓名杨仕琴学号54121022日期10.13实验题目编写三个类ComputerTime,Plane,Car007和一个接口程序Common.要求在未来如果增加第三种交通工具,不必修改之前任何程序,只要编写新的交通工具代码.从命令行输入ComputerTime的四个参数,第一个是交通工具的类型,二到三是整数A,B,C.程序初稿package unit4;public abstract class Common2 implements Common double A = 0;double B

8、= 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 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

9、 extends Common2 public 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 public void TestV();package unit4;import java.util.Scanne

10、r;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(car007 + v);package unit4;import java.util.Set;public class

11、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 TestV() double v = A + B + C;System.out.println(Plane + v);package unit4;public class ComputerTime public static void main(Str

12、ing args) throws InstantiationException,IllegalAccessException, ClassNotFoundException Plane com = (Plane) 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.T

13、estV();输出结果JAVA程序设计实验报告5姓名杨仕琴学号54121022日期10.13实验题目编写Calculater.java,实现一个简单计算器,要求布局,可以完成加减乘除。程序初稿package unit5;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;import java.math.*;import java.util.*;/* * Title: 用户应用界面计算器 * Description: 包含简易计算与功能更加强大的科学计算 * Copyright: Cop

14、yright (c) 2008 * Company: 四学 * 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();/产生首界面简易计算器 所遇问题及解决办法流布局方面。解决

15、办法:看书加上问老师调试后的程序代码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 boo

16、lean 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.setLayout(new

17、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.addActionListener(this); s4=new JButton( 4 ); s4.addActionListener(

18、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.addActionListener(this); s9=new JButton( 9 ); s9.addActionListener(this); s0=new JButton( 0 ); s0.addActionListener(this);

19、 b1=new JButton( + ); b1.addActionListener(this); b2=new JButton( - ); b2.addActionListener(this); b3=new JButton( * ); b3.addActionListener(this); b4=new JButton( / ); b4.addActionListener(this); f1=new JButton( . ); f1.addActionListener(this); f2=new JButton( = ); f2.addActionListener(this); jPane

20、l2.add(s1); jPanel2.add(s2); jPanel2.add(s3); jPanel2.add(b1); jPanel2.add(s4); jPanel2.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.CENT

21、ER); public void num(int i) String s = null; s=String.valueOf(i); if(end) /如果数字输入结束,则将文本框置零,重新输入 resultField.setText(0); end=false; if(resultField.getText().equals(0) /如果文本框的内容为零,则覆盖文本框的内容 resultField.setText(s); else /如果文本框的内容不为零,则在内容后面添加数字 str = resultField.getText() + s; resultField.setText(str);

22、 public void actionPerformed(ActionEvent e) /数字事件 if(e.getSource()=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.getSour

23、ce()=s8) num(8); else if(e.getSource()=s9) num(9); else if(e.getSource()=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.index

24、Of(.)= 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; else throw new MyException(i, sum);public static class MyException extends Exception public MyException(int i1, int s

25、um1) 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();输出结果JAVA程序设计实验报告7姓名杨仕琴学号54121022日期10.13实验题目程序初稿package unit7;public class Book private String name;private double si

26、nglePrice;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 getBo

27、ok() 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;import java.util.Iterator;import java.util.Map;import java.util.Scanner;public class ShopCart private Map book3 = new Hash

28、Map(); /每个购物车一个单独的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();System.out.

29、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.setSinglePrice(dou);book1.setBook(book2);boo

30、k1.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.getNum() * (book5.getBook().getSingle

31、Price();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(); /购物车中加东西HashMap book4=(HashMap)shop.getB

32、ook3(); shop.getSum(book4); /购物车算总价所遇问题及解决办法While 条件写错,找了好久。调试后的程序代码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 setSi

33、nglePrice(double singlePrice) 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 jav

34、a.util.HashMap;import 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对象,数目不

35、大于MaxOrderwhile (order = 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 Scanner(System.in);int

36、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();for (Iterator itera

37、tor = 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;public class ShopCar

38、tDemo public static void main(String args) ShopCart shop=new ShopCart();/新建一个购物车shop.addBooks(); /购物车中加东西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.*;impo

39、rt 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(t

40、xtMessage);this.add(btnRead);this.add(btnWrite);btnRead.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

41、e) JButton btn = (JButton)e.getSource();String fileName = 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() !=

42、 null)buffer.append(line + 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(); 所遇问题及解决办法文件读写的类太多,查

43、API查很久调试后的程序代码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(读取);b

44、tnWrite = new JButton(写入);this.setLayout(new FlowLayout();this.add(new JLabel(文件内容:);this.add(txtMessage);this.add(btnRead);this.add(btnWrite);btnRead.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 d

温馨提示

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

评论

0/150

提交评论