已阅读5页,还剩13页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验 3 面向对象程序设计(一)1.定义一个类MyValue,其中包括:用Value来保存一属性值;方法setValue设置Value,方法getValue获取Value,定义一个类UseValue,在该类的Main方法里面完成如下功能:创建一个MyValue类的对象MyValue;为MyValue对象中的Value赋值10;使用getValue方法获得MyValue对象中的数据并在屏幕上输出。【参考程序】class MyValueprivate int value;public void setvalue(int x)value=x;public int getValue()return value;public class UseValuepublic static void main(String args)MyValue MyV=new MyValue();MyV.setvalue(10);System.out.println(MyV.getValue();2.编写Java代码实现一个计数器类Computer,其中包括:用CountValue来保存计数器的当前值。方法Computer(int a)是构造方法并给CountValue赋初值。方法increment()计数器加一方法decrement()计数器减一方法reset()计数器清零使用计数器类创建一对象,该计数器对象当前值为10,调用三次increment(),输出计数器当前值,调用一次decrement(),输出计数器当前值,调用reset(), 输出计数器当前值.public class Computerpublic int CountValue; Computer (int a)CountValue=a;public void increment ()CountValue+;public void decrement ()CountValue-;public void reset ()CountValue=0;public static void main(String args)Computer MyV=new Computer(10);MyV.increment(); MyV.increment();MyV.increment();System.out.println(MyV. CountValue);MyV.decrement ();System.out.println(MyV. CountValue);MyV. reset ();System.out.println(MyV. CountValue);3. 定义一个名字为MyRectangle的矩形类,类中有4个私有的整型成员变量,分别是矩形的左上角坐标(xUp,yUp)和右下角坐标(xDown,yDown);类中定义了无参数的构造方法和有4个int参数的构造方法,用来初始化类对象。类中还有以下方法:getW() 计算矩形的宽度;getH() 计算矩形的高度;area() 计算矩形的面积;toString() 把矩形的宽、高和面积等信息作为一个字符串返回。编写应用程序使用MyRectangle类。【参考程序】class MyRectangleprivate int xUp,yUp,xDown,yDown;MyRectangle()xUp = 0; yUp = 0; xDown = 0; yDown = 0;MyRectangle(x1, y1, x2, y2 )xUp = x1; yUp = y1; xDown = x2; yDown = y2;public int getW()return xDown - xUp;public int getH()return yDown - yUp;public int area()return getW() * getH();public String toString() return 矩形宽: + getW() +矩形高: + getH() + 矩形面积:+area();public Class testpublic static void main(String args) MyRectangle rectangle = new MyRectangle(1,2,7,8);System.out.println(rectangle.toString();4. 设计一个表示用户的User类,类中的变量有用户名、口令(私有的)和记录用户个数的变量(静态的),定义类的3个构造方法(没有参数、有一个参数给用户名赋值、有两个参数给用户名和口令赋值)、获取和设置口令的方法、返回字符串表示的类信息的方法(包含用户名、口令)。编写应用程序测试User类。【参考程序】class User private String name, password; private static int number; User() name = null;password = null;n umber+; User(String n) name = n; password = null; number+; User(String n, String ps) name = n; password = ps; nmuber+ public String getPassword() /密码全部以明文操作,没有使用加密算法 return password; public setPassword(String ps) password = ps; public String toString() return 用户名:+ name +口令:+ password;public class Test User usr = new User(张三, 123456); usr.setPassword(abcdef); usr.toString();1、定义一个接口,接口中有四个抽象方法:求面积方法、求周长方法、显示面积方法及显示周长方法。定义Circle类和Rectangle类分别实现接口,在主类中实现显示圆和矩形的面积和周长。1.public interface J_Shape public abstract double mb_getArea();public abstract double mb_getPerimeter();public class J_Circle implements J_Shape public double m_radius;public J_Circle(double r)m_radius=r;public double mb_getArea()return(Math.PI*m_radius*m_radius);public double mb_getPerimeter()return(Math.PI*2*m_radius);public static void main(String args)J_Shape a=new J_Circle(5);System.out.println(the area of circle is: + a.mb_getArea();System.out.println(the perimeter of circle is + a.mb_getPerimeter();public class Rectangle implements J_Shape public double longs,wides;public Rectangle(double l,double w)longs=l;wides=w;public double mb_getArea() return(longs*wides);public double mb_getPerimeter() return (2*(longs+wides);public static void main(String args)J_Shape a=new Rectangle(2,3);System.out.println(the area of rectangle is:+a.mb_getArea();System.out.println(the perimeter of rectangle is:+a.mb_getPerimeter();2、重写上面的程序,要求矩型,圆的类放到不同的包中,用包的技术组织程序的设计。同时要求程序能从键盘上接受数据以便求解不同的几何图形的周长面积。提示:从键盘上输入双精度数的一种方法(程序片段) public interface J_Shape public abstract double mb_getArea();public abstract double mb_getPerimeter();import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class J_Square implements J_Shape public double radious;public J_Square(double x)radious=x;public double mb_getArea() return(Math.PI*radious*radious);public double mb_getPerimeter() return (Math.PI*radious*2);public static void main(String args)throws IOExceptionBufferedReader input=new BufferedReader(new InputStreamReader(System.in);System.out.print(Enter one number:);int number=Integer.parseInt(input.readLine();J_Shape a=new J_Square(number);System.out.println(the area of circle is:+a.mb_getArea();System.out.println(the perimeter of circle is:+a.mb_getPerimeter();import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Rectangle implements J_Shape public double longs,wides;public Rectangle(double l,double w)longs=l;wides=w;public double mb_getArea() return(longs*wides);public double mb_getPerimeter() return (2*(longs+wides);public static void main(String args)throws IOExceptionBufferedReader input=new BufferedReader(new InputStreamReader(System.in);System.out.print(Enter one number1:);int number1=Integer.parseInt(input.readLine();System.out.print(Enter one number2:);int number2=Integer.parseInt(input.readLine();J_Shape a=new Rectangle(number1,number2);System.out.println(the area of rectangle is:+a.mb_getArea();System.out.println(the perimeter of rectangle is:+a.mb_getPerimeter();实验 5字符串、数组与异常处理1、编写一程序,随机生成5个不大于100的整数存入一数组,计算该数组的平均值,输出该5个整数及平均值。public class Averagepublic static void main(String args) int intNumber;int i;intNumber = new int5;for ( i = 0 ; i intNumber.length ; i+)intNumberi = (int)(Math.random()*100);System.out.print( intNumberi + );System.out.println( nThe Average is : +GetAverage(intNumber) );static double GetAverage(int intNumber)int i,intTotal = 0;double dblResult;for ( i = 0 ; i = 0; i-) /反向输出数组System.out.print(si+); /*编写一程序,计算矩阵 A=7,9,4,5,6,8 与矩阵 B=9,5,2,8,5,9,7,2,4,7,5,8 *相乘把结果存入矩阵 C, 并在屏幕输出结果。*/ public class Example5_3 public static void main(String arg) int a=7,9,4,5,6,8; int b=9,5,2,8,5,9,7,2,4,7,5,8; int c=new int24; System.out.println(矩阵 A*B:); for(int i=0;i2;i+) for(int j=0;j4;j+) cij=0; for(int k=0;k3;k+) cij=cij+aik*bkj; System.out.print(cij+ ); System.out.println(); 实验 7图形用户界面1在应用程序窗体中安排两个文本框分别用来输入两个整数,两个按钮分别为“+”、“*”,一个结果标签。点击按纽“+”将两文本框的数据做加法运算;点击按钮“*”做乘法运算,将结果显示在标签中。 【参考程序】import java.awt.*; import java.awt.event.*; public class myFrame extends Frame implements ActionListener Label res; TextField f1,f2;public myFrame () f1 =new TextField(20); f2 =new TextField(20); Button b1=new Button(+); Button b2=new Button(*); res=new Label( 运算结果 ); setLayout(new GridLayout(3,2); add(f1); add(f2); add(b1); add(b2); add(res); b1.addActionListener(this); b2.addActionListener(this); public void actionPerformed(ActionEvent e) int x1=Integer.parseInt(f1.getText(); int x2=Integer.parseInt(f2.getText(); if (e.getActionCommand().equals(+) /区分用户点击的是哪个按钮 res.setText(+(x1+x2); else res.setText(+(x1*x2); public static void main(String args) Frame my= new myFrame(); my.setSize(200,200); my.setVisible(true); (1)设计一个在 Windows 系统中可以使用的“计算器”窗口并实现(、等)其功能。 (1) 【参考程序】import java.awt.BorderLayout;import java.awt.Color;import java.awt.FlowLayout;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextArea;public class calculator implements ActionListener JFrame frame=new JFrame(); private JButton b; private JButton b2; private JTextArea t; private JButton jbtnsqrt; private boolean flag=false; public calculator() jbtnsqrt=new JButton(Sqrt); b=new JButton16; b2=new JButton(C); String str=123+456-789x0.=/; for(int i=0;ib.length;i+) bi=new JButton(str.substring(i,i+1); private void setFontAndColor() Font f=new Font(宋体,Font.BOLD,18); t.setFont(f); for(int i=0;ib.length;i+) bi.setFont(f); private void init() t=new JTextArea(1,20); JPanel northPanel=new JPanel(); JPanel centerPanel=new JPanel(); northPanel.setLayout(new FlowLayout(); centerPanel.setLayout(new GridLayout(4,4); northPanel.add(t); northPanel.add(b2); northPanel.add(jbtnsqrt); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); b2.setBackground(Color.RED); b2.setForeground(Color.green); b2.addActionListener(this); jbtnsqrt.addActionListener(this); for(int i=0;ib.length;i+) centerPanel.add(bi); bi.addActionListener(this); frame.setLayout(new BorderLayout(); frame.add(northPanel,BorderLayout.NORTH); frame.add(centerPanel, BorderLayout.CENTER); setFontAndColor(); frame.setLocation(340,300); frame.setVisible(true); frame.pack(); frame.setResizable(false); public void actionPerformed(ActionEvent e) if(e.getSource()=b2) t.setText(null);/点击C时清空文本域 else if(e.getSource()=b14) /点击=时读取文本域的字符串 String s=t.getText().trim(); String rs; if(s.contains(+) rs=s.split(+); double d=Double.parseDouble(rs0)+Double.parseDouble(rs1); t.setText(String.valueOf(d); else if(s.contains(-) rs=s.split(-); double d=Double.parseDouble(rs0)-Double.parseDouble(rs1); t.setText(String.valueOf(d); else if(s.contains(x) rs=s.split(x); double d=Double.parseDouble(rs0)*Double.parseDouble(rs1); t.setText(String.valueOf(d); else if(s.contains(/) rs=s.split(/); double d=Double.parseDouble(rs0)/Double.parseDouble(rs1); t.setText(String.valueOf(d); flag=true; else if(e.getSource()=jbtnsqrt) String s=t.getText().trim(); double d=Math.sqrt(Double.parseDouble(s); t.setText(String.valueOf(d); else if(flag) t.setText(); / 不点击=和C时,将字符串追加在文本域中 t.append(e.getActionCommand(); flag=false; public static void main(String args) new calculator().init(); 3 实现一个简单的图像浏览器,部署“上一张”、“下一张”两个按钮,点击按钮可前后翻阅图片。【参考程序】import java.applet.*; import java.awt.*; import java.awt.event.*;public class ShowAnimator extends Applet implements ActionListener Image m_Images; /保存图片序列的Image数组 int totalImages = 18; /图片序列中的图片总数18 int currentImage = 0; /当前时刻应该显示图片序号 Button b1,b2; public void init() m_Images = new ImagetotalImages; for(int i=0; i0) currentImage = -currentImage; /上一张 else currentImage = +currentImage % totalImages; /下一张 repaint(); 设计一个表示二维平面上点的类Point,包含有表示坐标位置的protected类型的成员变量x和y,获取和设置x 和y值的public方法;class Pointprotected double x;protected double y;public Point()x=0;y=0;public Point(double x,double y)this.x=x;this.y=y;public double getX()return x;public void setX(double x)this.x=x;public double getY()return y;public void setY(double y)this.y=y;/*设计一个表示二维平面上圆的类Circle,它继承自类Point,还包含有表示圆半径的protected类型的成员变量r、 *获取和设置r值的public方法、计算圆面积的public方法*/class Circle extends Pointprotected double r;public Circle()r=0; public Circle(double r)this.r=r;public Circle(double x,double y,double r)this.r=r;this.x=x;this.y=y;public double getR()return r;public void setR(double r)this.r=r;public double area()return Math.PI*r*r;public class Testpublic static void main(String arg)Circle c=new Circle(8);System.out.println(圆中x轴坐标+c.getX();System.out.println(圆中y轴坐标+c.getY();System.out.println(半径+c.getR();System.out.println(圆面积+c.area(); 编写如下界面程序(P245),掌握基本组件的使用以及事件的处理。import java.awt.Container;import java.awt.FlowLayout;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;public class J_LabelFrame extends JFramepublic J_LabelFrame()super(框架和标签例程);String s=文本标签,文字在图标的左侧,文字在图标的下方;ImageIconic=null,new ImageIcon(imgl.gif),new ImageIcon(img2.gif);int ih=0,JLabel.LEFT,JLabel.CENTER;int iv=0,JLabel.CENTER,JLabel.BOTTOM;Container c=getContentPane();c.setLayout(new FlowLayout(FlowLayout.LEFT);for(int i=0;i0)aLabel.setHorizontalTextPosition(ihi);aLabel.setVerticalTextPosition(ivi);aLabel.setToolTipText(+(i+1)+个标签);c.add(aLabel);public static void main(String args)J_LabelFrame app=new J_LabelFrame();app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);app.setSize(360,150);app.setVisible(true);编写画图程序(P295),掌握监听器接口与事件适配器区别。import java.awt.Dimension;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.Graphics;import java.awt.Point;import java.util.Vector;import javax.swing.JPanel;public class J_Panel extends JPa
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年中国环境保护设备行业市场规模及投资前景预测分析报告
- 2026年中国玛钢衬塑管件行业市场前景预测及投资价值评估分析报告
- 武汉某国有企业招聘工程监理管理岗10人笔试考试参考试题及答案解析
- 2025重庆市开州区中医院公开招聘11人考试笔试备考题库及答案解析
- 2025年江西医学高等专科学校高层次人才招聘15人笔试考试参考试题及答案解析
- 2025重庆中医院第九批招聘计划笔试考试备考题库及答案解析
- 肾病中医健康科普
- 2025年道路维修改造施工合同模板
- 2026年四川中医药高等专科学校单招职业适应性考试必刷测试卷及答案1套
- 房地产市场报告 -2025年三季度深圳零售市场报告
- 基于项目式学习的高中数学建模活动教学设计研究
- 颅内压增高及脑疝教案及课件
- 《食育师能力培训与评价》
- 【MOOC】《数字电子技术基础》(北京交通大学)章节中国大学慕课答案
- 二零二四年度文化创意产品开发合作框架协议范本
- 《cA地基承载力》课件
- 考点五 地表形态的塑造-五年(2020-2024)高考地理真题专项分类汇编
- 医学检验技术生涯发展展示
- 大学生党规党纪培训
- 2024年医疗器械经营质量管理规范培训课件
- 电气工程及其自动化职业规划课件
评论
0/150
提交评论