《JAVA程序设计》实验报告.docx

上传人:大张伟 文档编号:11088337 上传时间:2021-06-29 格式:DOCX 页数:62 大小:95.92KB
返回 下载 相关 举报
《JAVA程序设计》实验报告.docx_第1页
第1页 / 共62页
《JAVA程序设计》实验报告.docx_第2页
第2页 / 共62页
《JAVA程序设计》实验报告.docx_第3页
第3页 / 共62页
《JAVA程序设计》实验报告.docx_第4页
第4页 / 共62页
《JAVA程序设计》实验报告.docx_第5页
第5页 / 共62页
点击查看更多>>
资源描述

《《JAVA程序设计》实验报告.docx》由会员分享,可在线阅读,更多相关《《JAVA程序设计》实验报告.docx(62页珍藏版)》请在三一文库上搜索。

1、JAV阳序设计实验报告 1姓名学号日期实验题目输由自己姓名年龄身局体重,通过控制台和Eclipse运行程序初稿package schooltext;public class MyselfPrint public static void main(String口 args) System. out .println(D?o?e ?立?e ? ?o18?e i ?o160cm?o100?):所遇问题及解决办法Hcrosoft Ulndows KF 1版本 5.1,2600J 版权所有 1935-2001 Hlcposoft Corp.C; xDocuRents呈共七1口00、陈丹)初己心*中后后1

2、班:1门1:1. RyselFpiintJ不是内部或外部命令口也不是可运行的程序或批处理文件.C:良口A &C七七工口93陈丹调试后的程序代码package schooltext;public class MyselfPrint public static void main(String口 args) System. out .println(D?o?e ?u ? ?e ? ?o18?e i ?o160cm?o100?!输生结果D?o?e ?u ? ? ? e ? ?o18?e i ?o160cm? i ?o100?JAV阳序设计实验报告 2姓名杨仕琴学号54121022日期10.13实验题

3、目打印水仙花数程序初稿public class testl 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);所遇问题及解决办法第一次输出结果:03701371153407调试后的程序代码public stat

4、ic void main(Strfor ( int j1 = 0; j1 10; jfor ( int j2 = 0; j2 1for ( int j3 = 1; j3int i=(j1+j2*10+jing args) 1+) 0; j2+) 10; j3+) 3*100);int O=(j1*j1*j1+j2*j2*j2+j3*j3*j3);if (i=o) System. out .println(i);输生结果370371153407JAV阳序设计实验报告 3姓名杨仕琴学号54121022日期10.13实验题目编写三个类,按照命名空间规则分别放到不同包中,在代码中调用其它包中的类,并查

5、看类文件在系统中的组织方式程序初稿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 .println( i am pig from package 3);pa

6、ckage 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(/).g etPath();System.out.println(b.getClass().getResource(/).g etPath();System.out.

7、println(p.getClass().getResource(/).g etPath();所遇问题解决办法调试后的程序代码输生结果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.yangs

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

9、etA( 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 ?u ? e e ?o+ v);package unit4;public class Car0072 extends Comm

10、on2 public void TestV() double v;v = A * B / C;System. out .println( car007 ?u ?e e ?o+ 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.Scann

11、er;public classCar007 implements Common doubleA =0;doubleB =0;doubleC =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;e ?o+ v);v = A * B / C;System. out .println( car007 ?u ?epackage unit4;import java.util.Set;dou

12、bleA = 0;doubleB = 0;doubleC = 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() e e ?o+ v);double v = A + B + C;System. out .println( Plane ?u ?package unit4;public class ComputerTime public staticInstantiationException,void

13、main(String口args)throwsIllegalAccessException,ClassNotFoundException Planecom=Class. forName (unit4.Plane ).newInstance();(Plane)double A = Double.double B = Double.double C = Double.parseDouble (args1);parseDouble (args2);parseDouble (args3);com.setA(A);com.setB(B);com.setC(C);com.TestV();输生结果1ITGJ

14、0 修,ne2 jsV3=E Console 3 J ComputerTime (1) Java Application D:JJAV阳序设计实验报告 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:用户应用界面计算器*

15、 Description:包含简易计算与功能更加强大的科学计算* Copyright: Copyright (c) 2008* Company:四 学 * author * 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 EasyFr

16、ame();/广生首界面简易计算器所遇问题及解决办法流布局方面。解决办法:看书加上向老师调试后的程序代码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,s

17、0,b1,b2,b3,b4,f1,f2;private boolean 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();jP

18、anel2.setLayout(new GridLayout(4,4);resultField=new JTextField(0);jPanel1.add(resultField);con.add(jPanel1,BorderLayout.NORTH);s1=new JButton( 1 );s2=new JButton( 2 );s3=new JButton( 3 );s4=new JButton( 4 );s5=new JButton( 5 );s6=new JButton( 6 );s1.addActionListener(this);s2.addActionListener(this)

19、;s3.addActionListener(this);s4.addActionListener(this);s5.addActionListener(this);s6.addActionListener(this);s7=new JButton( 7 );s8=new JButton( 8 );s9=new JButton( 9 );s0=new JButton( 0 );b1=new JButton( + );b2=new JButton(-);b3=new JButton( * );b4=new JButton( / );f1=new JButton(.);f2=new JButton(

20、=);jPanel2.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);s7.addActionListener(this);s8.addActionListener(this);s9.addActionListene

21、r(this);s0.addActionListener(this);b1.addActionListener(this);b2.addActionListener(this);b3.addActionListener(this);b4.addActionListener(this);f1.addActionListener(this);f2.addActionListener(this);jPanel2.add(f2);jPanel2.add(b4);con.add(jPanel2,BorderLayout.CENTER);public void num(int i)String s = n

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

23、etSource(尸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.getSource()=s0

24、)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(.)= 0) try Scanner sc = new Scanner(System.in);i = sc.nextInt(); catch (InputMismatchExc

25、eption 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 suml) throws MyException i1 = 0;i = i1;sum = sum1;goOn();public static void main(String口 arg

26、s) Sumget sumget = new Sumget();try sumget.goOn(); catch (MyException e) e.printStackTrace();输生结果7 .java 72Java ion .java lonijavs uterTime.jav u+erTirrie av用 Ljava!itj ava tor 排口 Sumget.MyException Uava Application DiJavaJavare 1.6.0_02111qQ1 .L,2323-22I r 11 hL.34怙99JAV阳序设计实验报告 7姓名杨仕琴学号54121022日期1

27、0.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) this . name = name;singlePrice) public void setSinglePrice( double this . singlePrice =

28、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;import java.util.Iterator;i

29、mport 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 (order = MaxOrder) Syste

30、m.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 i = sc3.nextInt();Books book1 = new

31、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 iterator = bookss.keySet().iterator();计

32、erator.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 ShopCartDemo public static void main(String口 ar

33、gs) ShopCart shop=new ShopCart();/ 新建一个购物车shop.addBooks();/购物车中加东西HashMap book4=(HashMap)shop.getBook3();shop.getSum(book4); / 购物车算总价所遇问题及解决办法While条件写错,找了好久。1Exception in threadjaiua Lanq - CIre 15c自石: java . lang. Integer cannot b cast toat unit7* Shope art .get sum (511口 口匚mrt .)at unit?.ShopCartD

34、emo口(Shq-CmrtDemo.ja一:1S)调试后的程序代码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) this . name = name;singlePrice) public void setSinglePrice( double

35、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;import

36、 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 (or

37、der = 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 i = sc3.nextInt

38、();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 iterator = bookss.keySet().iterator();计erator.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.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1