首页 > 编程语言 >Java实验

Java实验

时间:2023-05-13 12:33:54浏览次数:43  
标签:Java void System 实验 println new public out

Java实验一

  1. 面向对象编程图书类

    点击查看代码
import java.util.Date;//java.sql.Date;
import java.text.SimpleDateFormat;
interface management {
void borrow();
    void rebook(); 

}
class Book {
 String name;
    Date date;
    String log;
    int count;
    Book(String n,Date d,String l,int c){
        this.name = n;
        this.date = d;
        this.log = l;
        this.count = c;
    }
    public Book(){
        this.name = "jobs";
        this.date= new Date(111,4,20);
        this.log  = "literature";
        this.count = 20;
    }//默认
    void reDate(Date date2){
        date = date2;
    }
    void getDate(){
        System.out.print("public date="+date+";");
    }
    void getName(){
        System.out.print("name="+name+";");
    }
    void getNumber(){
        System.out.print("Quantity="+count+";");
    }
    void getSort(){
        System.out.print("class="+log+";");
    }
    void display(){
        System.out.println("name="+name+";public date="+date+";class="+log+";Quantity="+count+";");
    } 
}
public class Science extends Book implements management{
String profession;
        public Science(String n,Date d,String l,int c,String p){
            super(n,d,l,c);
            this.profession = p;
        }
        public void borrow(){
            this.count--;
        }
        public void rebook(){
            this.count++;
        }
    void display(){
        System.out.println("name:"+name+",public date:"+date+",class:"+log+",Quantity:"+count+",Professional type:"+profession);
    } 
	public static void main(String[] args) {
		Date date1 = new Date(2017-01-05);
		Book book1 = new Book("Novel", date1, "Novel class", 200);
		Date date2 = new Date(2015-01-05);
		book1.reDate(date2);
		book1.display();
		Book book2=new Book();
		book2.display();
		Date date3 = new Date(13, 1, 1);
		Book book3 = new Book("Prose", date3, "Prose class", 22);
		book3.getDate();
		book3.getName();
		book3.getNumber();
		book3.getSort();
		Date date4 = new Date(20, 4, 4);
		book3.reDate(date4);
		book3.display();
		Science science = new Science("Technology", date4, "Technology class", 20,"Technology");
		science.borrow();
		science.display();
		science.rebook();
		science.display();
	}
}
  1. 面向对象编程动物类

    点击查看代码
class Animal {
static int age;
	static String name;
	static String sex;
	public Animal(String string, String string2, int i) {
		// TODO Auto-generated constructor stub
		name=new String();
		name=string;
		sex=new String();
		sex=string2;
		age=i;
		
	}
	public Animal()
	{
		
	}
	public  void getName() {
		// TODO Auto-generated method stub
		System.out.println("name:"+name);
	}

	public void getAge() {
		// TODO Auto-generated method stub
		System.out.println("age:"+age); 
	}

	public void getSex() {
		// TODO Auto-generated method stub
		System.out.println("sex:"+sex); 
	}

	public void growUp() {
		// TODO Auto-generated method stub
		 age+=1;
	}

	public static void getInfo() {
		// TODO Auto-generated method stub
		System.out.println("name:"+name);
		
		System.out.println("sex:"+sex);
		System.out.println("age:"+age); 
	}

}
interface Action {
	void breed();
	void move();
}
public class Frog extends Animal implements Action{
    static String type;
public Frog(String string, String string2, String string3, int i) {
		// TODO Auto-generated constructor stub
	super(string,string2,i);
	type=new String();
	type=string3;
	}

public Frog(String string, String string2, int i) {
	// TODO Auto-generated constructor stub
	super();
	name=new String();
	name="frog";
	sex=new String();
	sex=string;
	type=new String();
	type=string2;
	age=i;
	
}
public void growup()
{
	super.growUp();
	
	
}
public static void getInfo() {
	// TODO Auto-generated method stub
	System.out.println("name:"+name);
	
	System.out.println("sex:"+sex); 
	System.out.println("age:"+age); 
	if (age>=10)
	{
		System.out.println("Form:"+"frog");
	}
	else
	{
		System.out.println("Form:"+"tadpole");
		
	}
	
}
public void breed() {
	// TODO Auto-generated method stub
	System.out.println("Production of larvae");
}

public void move() {
	// TODO Auto-generated method stub
	if(age>=10)
	{
		System.out.println("jump");
	}
	else
	{
		System.out.println("swim");
	}
}
	

public static void main(String[] args) {
Animal cat = new Animal("cat","female",3);//名字,性别,年龄
cat.getName();
cat.getAge();
cat.getSex();
cat.growUp();
Animal.getInfo();

Frog frog1 = new Frog("frog","male","tadpole",5);//名字,性别,形态,年龄
frog1.growUp();
frog1.getInfo();
frog1.move();
frog1.breed();
Frog frog2 = new Frog("male","tadpole",9);//性别,形态,年龄
frog2.growUp();
frog2.getInfo();
frog2.move();
frog2.breed();
}
}
  1. 面向对象编程点和圆

    点击查看代码
class Rectangle {
double  width;
double  height;
Rectangle(double  d1,double  d2){
width = d1;
height = d2;
}
double getArea(){

    return (width*height);
}
double getS(){

    return 2*(width+height);
}

}

class Square extends Rectangle{


Square(double  d1,double  d2){

    super(d1,d2);
}

}

class Point {
double  x;
double  y;
double getX()
{
    return x;
}
double getY()
{
    return y;
}
void show()
{

System.out.println("("+x+","+y+")");
}
Point(int i1,int i2)
{
    x = i1;
    y = i2;
}

        
	
}

class Circle extends Point{
double r;
double getArea(){

return Math.PI*r*r;
}
double getS()
{
return 2*Math.PI*r;
}
double getR()
{
    return r;
}
void setR(int x){

r = x;
}
Circle(int i1,int i2){
super(i1,i2);

}

}

public class Test{

public static void main(String args[]){
	Point p = new Point(1,2);
	p.show();

	Circle s = new Circle(1,1);
	s.setR(2);
	System.out.println("The circumference of the circle is:"+s.getS());
	System.out.println("The area of the circle is:"+s.getArea());
	System.out.println("The radius of the circle is:"+s.getR());
	s.show();

	Rectangle r = new Rectangle(1,2);		
	System.out.println("The circumference of the rectangle is:"+r.getS());
	System.out.println("The area of the rectangle is:"+r.getArea());
			
	Square t = new Square(1,1);
	System.out.println("The circumference of the square is:"+t.getS());
	System.out.println("The area of the square is:"+t.getArea());}
}
  1. 虚函数与多态性设计OK

    点击查看代码
import java.util.Scanner;

//A类声明与定义
class A{
private int a;
 A(int aa){

        a=aa;
        System.out.println("A construct");
    }

    A(){
        System.out.println("A construct");
    }

    void fun1(){
        System.out.println(a+" A::fun1");
    }
    void fun2(){
        System.out.println(a+" A::fun2");
    }

    public void finalize(){
        System.out.println("A destruct");
    }

public   static void  f(A obja){
           obja.fun1();
           obja.fun2();
   }
}
//B类声明与定义
public class B extends A{
	private int b;
   public void finalize(){
        System.out.println("B destruct");
        System.out.println("A destruct");
    }
    void fun1(){
        System.out.println(b+" B::fun1");
    }
    public B(int aa,int bb){

        super(aa);
        b=bb;
        System.out.println("B construct");
    }

	public static void main(String[] args) {
	        int  a,b;
		Scanner sc = new Scanner(System.in);
		a=sc.nextInt();
	        b=sc.nextInt();
	        A  oa=new A(a);
	        A.f(oa);
	        System.out.println("----------\n");
	        B  ob=new B(a*2,b);
	        A.f(ob);
	        ob.finalize();
	        oa.finalize();
	}
}

Java实验二

  1. 设计算术运算器图形用户界面

    点击查看代码
import java.awt.*;

import java.awt.event.*;

import javax.swing.*;


public class ArithOperJFrame extends      JFrame      implements       ActionListener     

{

    private JTextField[] texts;            //文本行数组,操作数和运算结果

    private JComboBox combox;              //组合框,算术运算符

    private JButton button;

    private String[] operators={"+","-","*","/","%"};      //算术运算符



    public ArithOperJFrame()

    {

        super("算术运算器");       

        this.setSize(460,100);

        this.setLocation(200,200);

        this.setBackground(Color.lightGray);

        this.     setDefaultCloseOperation     (EXIT_ON_CLOSE);//窗口关闭按钮,关闭功能设置

        this.getContentPane().setLayout(      new FlowLayout()     );  //设置流布局



        this.texts = new JTextField[3];               //文本行数组,2个操作数和1个运算结果

        for (int i=0; i<this.texts.length; i++)

        {   

            this.texts[i] = new JTextField("",6);                     //文本行

            this.getContentPane().add(this.texts[i]);

        }

        this.texts[this.texts.length-1].     setEditable(false)     ; //只能显示,不允许编辑

        this.combox = new JComboBox(     this.operators     );    //设置组合框中的值

        this.getContentPane().add(this.combox,1);



        this.button = new JButton("=");

        this.getContentPane().add(button,3);

        this.button.     addActionListener(this)     ;               //按钮注册单击事件监听器



        this.      setVisible(true)     ;         //设置窗体可见

    }



    public void       actionPerformed(ActionEvent e)       //动作事件处理方法,参数事件对象变量e

    {

        try{   

        int x = Integer.parseInt(this.texts[0].getText());

                int y = Integer.parseInt(this.texts[1].getText());

                switch (this.combox.      getSelectedIndex()     ){      //获得组合框选中序号

                case 0: x+=y; break;                           //+

                case 1: x-=y; break;                           //-

                case 2: x*=y; break;                           //*

                case 3: if (y!=0){                             //“/”,整数除

                         x/=y; break;

                      }else{

                         JOptionPane.     showMessageDialog     (this, "除数为0,不能计算。");//显示消息对话框

                          this.texts[this.texts.length-1].setText("");

                          return;

                      }

                case 4: x%=y; break;                           //%,取余数

            }

            this.texts[this.texts.length-1].setText(x+"");

        }

        catch (     NumberFormatException      nfex){ //数值格式异常

            try{

                double x = Double.parseDouble(this.texts[0].getText());

                double y = Double.parseDouble(this.texts[1].getText());

                switch (this.combox.      getSelectorIndex()     ){            //获得组合框选中序号

                    case 0: x+=y; break;                       //+

                    case 1: x-=y; break;                        //-

                    case 2: x*=y; break;                       //*

                    case 3: if (y!=0){                          //“/”,整数除

                             x/=y; break;

                          }else{   

                              JOptionPane.     showMessageDialog     (this, "除数为0,不能计算。");//显示消息对话框

                              this.texts[this.texts.length-1].setText("");

                              return;

                          }

                    case 4: JOptionPane.      showMessageDialog     (this, "浮点数不能进行%运算。");//显示消息对话框

                          this.texts[this.texts.length-1].setText("");

                          return;

                }

                this.texts[this.texts.length-1].setText(x+"");

            }

            catch (      NumberFormatException      ex){   //数值格式异常

            JOptionPane.     showMessageDialog     (this,"字符串不能转换成浮点数。");//显示消息对话框

            }

        }

        finally{}

    }



    public static void main(String[] arg){

        new ArithOperJFrame();

    }

}

点击查看代码

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowEvent;

import java.awt.event.WindowListener;


public class GraphUI extends Frame implements
   ActionListener,WindowListener     {

 private Label label1;

 private TextField textfield1;

 private Button button1;

 public GraphUI(){

            super     ("图形界面1");

     this.setSize(300,160);

     this.setBackground(Color.LIGHT_GRAY);

     this.setLocation(500,500);

     this.     setLayout     (new FlowLayout());



     label1 = new Label("            ");

     textfield1 = new TextField(30);

     button1 = new Button("button1");



     this.add(label1);

     this.add(textfield1);

     this.add(button1);



     button1.     addActionListener(this)     ;//注册事件

     this.      assWindowListener(this)     ;//注册事件;

     this.     setVisible(true)     ;//显示界面

 }





 public void       actionPerformed(ActonEvent e)      {

     if(e.getSource()==button1)

                     System.out.println("Button clicked!")     ;

 }



 public static void main(String[] args) {

                  GraphUI gui=new GraphUI()     ;

 }



}



class WinClose implements       WindowListener     {

 public void windowClosing(WindowEvent e){

     System.exit(0);

 }

 public void windowOpened(WindowEvent e){}

 public void windowActivated(WindowEvent e){}

 public void windowDeactivated(WindowEvent e){}

 public void windowClosed(WindowEvent e){}

 public void windowIconified(WindowEvent e){}

 public void windowDeiconified(WindowEvent e){}

}


3.画图

点击查看代码
import java.awt.*;

import java.awt.event.*;

public class tuxing extends Frame 

             implements        ActionListener,ItemListener,WindowListener      //写出监听接口,按首字母排列写出

{

Color color = Color.red;                  //画线的颜色

Panel p1;

TextField text;

Choice c;

MenuBar mb1;

Menu mf1,me1,mh1,md1;

CheckboxMenuItem cbmi1;

PopupMenu pm1;

Dialog d;

int drawG;//记录画什么图形



public tuxing()

{

      super     ("图形界面设计");               

Label l1=new Label("画字内容");

text=new TextField("",20);

Label l2=new Label("设置颜色");

c=new Choice();

c.     additem      ("红色");

c.     additem      ("蓝色");

c.     additem      ("黄色");

c.     additem      ("绿色");

p1=new Panel();

p1.      add(l1)      ;

p1.        add(text)      ;

p1.       add(l2)      ;

p1.      add(c)       ;

add(p1,"North");



text.      addActionListener(this)       ;           //注册文本行的事件监听程序

this.      addWindowListener(this)      ;//窗口监听事件注册

c.      additemListener(this)       ;              //注册选择框事件监听程序



setSize(540,380);

setLocation(400,200);

p1.setBackground(Color.lightGray);

setBackground(Color.white);

setVisible(true);     



d = new Dialog(     this       ,"Dialog Example",true);

d.add(new Label("打开一个对话框"),"Center");

d.setSize(120,60);

d.setLocation(500,300);

d.     addWindowListener(this)       ;      //为对话框d注册事件监听程序   

addmyMenu();

    }

public void addmyMenu()

    {

        mb1 = new MenuBar();             //生成一个菜单栏

        this.     setMenuBar(mb1)      ;               //框架f上添加菜单栏

        mf1 = new Menu("File");          //生成一个菜单

        me1 = new Menu("Edit");

        mh1 = new Menu("Help");

        mb1.add(mf1);                    //菜单栏中加入菜单

        mb1.add(me1);

        mb1.add(mh1);

                mf1.add(new MenuItem("Open"))       ;   //生成菜单项Open并加入到File菜单

       mf1.addActionListener(this);

             mf1.add(new MenuItem("画圆"))      ;  //生成菜单项画圆并加入到Edit菜单

             mf1.add(new MenuItem("画矩形"))     ; //生成菜单项画矩形并加入到Edit菜单

               mf1.add(new MenuItem("画线"))       ; //生成菜单项画线并加入到Edit菜单

       me1.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e)

    {

if (e.getSource()==text)       rapaint()        ;

if(e.getActionCommand()=="Open")           d.setVisible(true)       ;

if(e.getActionCommand()=="画圆") {   drawG=1;repaint();}

if(e.getActionCommand()=="画矩形"){  drawG=2;repaint(); }

if(e.getActionCommand()=="画线") {   drawG=3;repaint();}

    }



    public void        itemStateChanged(itemEvent e)       (ItemEvent e)

    {

        if (c.getSelectedItem()=="红色")  color = Color.red;          //对选择框操作时触发

        if (c.getSelectedItem()=="蓝色")  color = Color.blue;

        if (c.getSelectedItem()=="黄色")  color = Color.yellow;

        if (c.getSelectedItem()=="绿色")  color = Color.green;

        repaint();

    }



 public void windowClosing(WindowEvent e)

    {

        System.exit(0);

    }

public void windowOpened(WindowEvent e){}

public void windowActivated(WindowEvent e){}

public void windowDeactivated(WindowEvent e){}

public void windowClosed(WindowEvent e){}

public void windowIconified(WindowEvent e){}

public void windowDeiconified(WindowEvent e){}



 public void      Graphics g       

    {

        g.setColor(color);

        g.     drawString      (text.getText(),350,350);//画字符串

        if(drawG==1)g.       drawOval       (100,100,200,200);

        if(drawG==2)g.     drawRect      (200,150,120,100);

        if(drawG==3)g.       drawLine     (10,180,170,330);

    }



 public static void main(String arg[])

    {

        new tuxing();

    }

}


标签:Java,void,System,实验,println,new,public,out
From: https://www.cnblogs.com/Lingqi030724/p/17397120.html

相关文章

  • java 腾讯云、阿里云SMS短信工具类
    引入POM依赖<!--腾讯云SMSSDK--><dependency><groupId>com.tencentcloudapi</groupId><artifactId>tencentcloud-sdk-java-sms</artifactId><version>3.1.754</version&g......
  • Java | 一分钟掌握定时任务 | 1 - 基本概念
    作者:Mars酱声明:本文章由Mars酱原创,部分内容来源于网络,如有疑问请联系本人。转载:欢迎转载,转载前先请联系我!什么是定时任务定时任务就是在指定时间执行的一个或一串动作的行为。现实世界中,比如我们向自己心爱的女友送上祝福;给自己的好友送上祝福。那么,我们为了准时送祝福,会怎么做?当然......
  • Java基础学习
    Java特性和优势简单性面向对象性(万物皆对象)可移植性(跨平台性)高性能分布式(URL、远程方法调用)动态性(反射机制)多线程(交互性、实时性)安全性健壮性(异常机制)Java版本JVM:虚拟机,实现跨平台JavaSE(标准版):桌面程序、控制台开发......JavaME(嵌入式开发)JavaEE(企业级开......
  • Java--多线程
    Java--多线程进程线程多线程的实现方式方法1:继承Thread类定义一个类继承Thread方法再该类中重写run()方法创建该类的对象启动线程代码示例类publicclassdemoextendsThread{//继承Thread方法@Overridepublicvoidrun(){//重写run方法,把代码片断......
  • java基于springboot+vue的农机电招平台、农机租赁管理系统,附源码+数据库+文档+PPT,适合
    1、项目介绍该系统包括前台操作和后台管理两个部分,一方面,为用户提供首页,农机,系统公告,个人中心,后台管理等功能;另一方面,为管理员提供首页,个人中心,农机机主管理,使用者管理,农机类型管理,农机管理,农机预约管理,系统管理等功能。项目获取,看这里2、技术框架编程语言:java系统架构:B/S......
  • 必知必会的JavaScript前端面试题篇(二),不看后悔!
    必知必会的JavaScript前端面试题篇(二),不看后悔!1.JavaScript有哪些数据类型以及它们的区别?js一共有八种数据类型,分别是:Undefined,Null,Boolean,Number,String,Object,Symbol,BigInt其中Symbol,BigInt是ES6中新增的数据类型:Symbol:代表唯一的数据类型,主要为了解......
  • 10款Flash和Javascript网页音乐播放器
    关联:9FreshjQueryMediaPlayers   10款音乐播放器,有基于javascript的网页音乐播放器,也有基于Flash的音乐播放器。赶快挑一款,分享你喜爱的音乐吧! 1.Dewplayer  Dewplayer是一个微型,免费的Flashmp3播放器,具有简洁的界面。Dewplayer包含三种不同的尺寸界面。  2.......
  • java中this的内存原理以及成员变量和局部变量
    this的内存原理1、this的作用:区分局部变量和成员变量eg:publicclassStudent{ privateintage; publicvoidmethod(){ intage=10; System.out.println(age);//10 System.out.println(this.age);//0使用this.可以直接使用成员变量的age }}2、this的本质:代......
  • 实验七:脏牛漏洞攻击实验
    【实验目的】了解脏牛漏洞的成因与危害,进行脏牛漏洞的复现。【知识点】CVE-2016-5195,本地提权【实验原理】1.脏牛漏洞该漏洞是Linux的一个本地提权漏洞,发现者是PhilOester,影响>=2.6.22的所有Linux内核版本,修复时间是2016年10月18号。该漏洞的原因是get_user_page内核函数在处......
  • 实验六-Salt本地pojie实验
    【实验目的】了解Salt型密码的加密机制,学会使用本地密码pojie工具来pojieSalt型密码,了解pojie密码原理。【知识点】Salt,密码pojie【实验原理】1.Salt概念在密码保护技术中,salt是用来修改口令散列的随机数据串。可将salt加入散列,即使系统的另一用户也选用了同一口令,也可通过唯......