Java100行以上源代码,至少五个class以及一个interface,可以简单点?

下面是一个可能的Java源代码,它包含了一个接口租冲薯(Shape)和五个类(Circle, Rectangle, Triangle, Square 和 Main)。它的功能是计算不同形状的面积和周长。

//定义一个接口Shape,有两判指个抽象方法:getArea()和getPerimeter()interface Shape { double getArea() double getPerimeter()

}//定义一个类Circle,实现Shape接口class Circle implements Shape { //定义一个私有属性radius,表示圆的半径

private double radius //定义一个公有构造方法,用于初始化radius

public Circle(double radius) {this.radius = radius

} //实现getArea()方法,返回圆的面积

public double getArea() {return Math.PI * radius * radius

} //实现getPerimeter()方法,返回圆的周长

public double getPerimeter() {return Math.PI * radius * 2

}

}//定义一个类Rectangle,实现Shape接口class Rectangle implements Shape { //定义两个私有属性width和height,表示矩形的宽度和高度

private double width private double height //定义一个公有构造方法,用于初始化width和height

public Rectangle(double width, double height) {this.width = width this.height = height

} //实现getArea()方法,返回矩形的面积

public double getArea() {return width * height

} //实现getPerimeter()方法,返回矩形的周长

public double getPerimeter() { return (width + height) *2

}

}//定义一个类Triangle,实现Shape接口class Triangle implements Shape { //定义三个私有属性a,b,c表示三角形的三条边长

private double a private double b private double c //定义一个公有构造方法,用于初始化a,b,c,并检查是否满足三角形条件(任意两边之和大于第三边)

public Triangle(double a, double b, double c) throws Exception{ if (a + b >c &&a + c >b &&b + c >a) {

this.a = a this.b = b

this.c = c

} else {

throw new Exception("Invalid triangle")

}

} //实现getArea()方法,返回三角形的面积(使用海伦公式)

public double getArea() { //计算半周长p

double p = (a + b + c) /2//计算并返回面积s(使用Math.sqrt()函数求平方根)

return Math.sqrt(p * (p - a) * (p - b) * (p - c))

}//实现getPerimeter()方法,返回三角形的周长

public double getPerimeter(){ return a + b + c

}

}//定义一个类Square,继承Rectangle类,并重写构造方法和toString()方法class Square extends Rectangle { //重写构造方法,在调用父类构造方法时传入相弊者同的参数side作为width和height

public Square(double side){ super(side, side)

} //重写toString()方法,在原来基础上加上"Square:"前缀,并只显示side属性而不显示width和height属性(使用String.format()函数格式化字符串)

@Override

public String toString(){ return String.format("Square: side=%.2f", super.width) /* 或者直接使用super.getPerimeter()/4作为side */

/* return String.format("Square: side=%.2f", super.getPerimeter()/4)*/

/* 注意:不能直接访问super.side属性,

import java.awt.*

import javax.swing.*

@SuppressWarnings("serial")

public class MainClass extends JFrame {

 ControlSnake control

 Toolkit kit

 Dimension dimen

 public static void main(String[] args) {

  new MainClass("my snake")

 }

 public MainClass(String s) {

  super(s)

  control = new ControlSnake()

  control.setFocusable(true)

  kit = Toolkit.getDefaultToolkit()

  dimen = kit.getScreenSize()

  add(control)

  setLayout(new BorderLayout())

  setLocation(dimen.width / 3, dimen.height / 3)// dimen.width/3,dimen.height/3

  setSize(FWIDTH, FHEIGHT)

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

  setResizable(false)

  setVisible(true)

 }

 public static final int FWIDTH = 315

 public static final int FHEIGHT = 380

}

import java.util.*

import java.awt.*

import java.awt.event.*

import javax.swing.*

import javax.swing.Timer

import java.util.Random

@SuppressWarnings("serial")

public class ControlSnake extends JPanel implements ActionListener {

 Random rand

 ArrayList<Point> list, listBody

 String str, str1

 static boolean key

 int x, y, dx, dy, fx, fy, flag

 int snakeBody

 int speed

 public ControlSnake() {

  snakeBody = 1

  str = "上下左右方向键控制 P键暂停..."

  str1 = "现在的长度为:" + snakeBody

  key = true

  flag = 1

  speed = 700

  rand 御磨= new Random()

  list = new ArrayList<Point>()

  listBody = new ArrayList<Point>()

  x = 5

  y = 5

  list.add(new Point(x, y))

  listBody.add(list.get(0))

  dx = 10

  dy = 0

  fx = rand.nextInt(30) * 10 + 5// 2

  fy = rand.nextInt(30) * 10 + 5// 2

  setBackground(Color.WHITE)

  setSize(new Dimension(318, 380))

  final Timer time = new Timer(speed, this)

 迟镇 time.start()

  addKeyListener(new KeyAdapter() {

   public void keyPressed(KeyEvent e) {

    if (e.getKeyCode() == 37) {

     dx = -10

     dy = 0

    } else if (e.getKeyCode() == 38) {

     dx = 0

     dy = -10

    } else if (e.getKeyCode() == 39) {

     dx = 10

     dy = 0

    } else if (e.getKeyCode() == 40) {

     dx = 0

     dy = 镇旦斗10

    } else if (e.getKeyCode() == 80) {

     if (flag % 2 == 1) {

      time.stop()

     }

     if (flag % 2 == 0) {

      time.start()

     }

     flag++

    }

   }

  })

 }

 public void paint(Graphics g) {

  g.setColor(Color.WHITE)

  g.fillRect(0, 0, 400, 400)

  g.setColor(Color.DARK_GRAY)

  g.drawLine(3, 3, 305, 3)

  g.drawLine(3, 3, 3, 305)

  g.drawLine(305, 3, 305, 305)

  g.drawLine(3, 305, 305, 305)

  g.setColor(Color.PINK)

  for (int i = 0 i < listBody.size() i++) {

   g.fillRect(listBody.get(i).x, listBody.get(i).y, 9, 9)

  }

  g.fillRect(x, y, 9, 9)

  g.setColor(Color.ORANGE)

  g.fillRect(fx, fy, 9, 9)

  g.setColor(Color.DARK_GRAY)

  str1 = "现在的长度为:" + snakeBody

  g.drawString(str, 10, 320)

  g.drawString(str1, 10, 335)

 }

 public void actionPerformed(ActionEvent e) {

  x += dx

  y += dy

  if (makeOut() == false) {

   JOptionPane.showMessageDialog(null, "重新开始......")

   speed = 700

   snakeBody = 1

   x = 5

   y = 5

   list.clear()

   list.add(new Point(x, y))

   listBody.clear()

   listBody.add(list.get(0))

   dx = 10

   dy = 0

  }

  addPoint(x, y)

  if (x == fx && y == fy) {

   speed = (int) (speed * 0.8)//速度增加参数

   if (speed < 200) {

    speed = 100

   }

   fx = rand.nextInt(30) * 10 + 5// 2

   fy = rand.nextInt(30) * 10 + 5// 2

   snakeBody++// 2

  } // 2

  repaint()

 }

 public void addPoint(int xx, int yy) {

  // 动态的记录最新发生的50步以内的移动过的坐标

  // 并画出最新的snakeBody

  if (list.size() < 100) {//蛇身长度最长为100

   list.add(new Point(xx, yy))

  } else {

   list.remove(0)

   list.add(new Point(xx, yy))

  }

  if (snakeBody == 1) {

   listBody.remove(0)

   listBody.add(0, list.get(list.size() - 1))

  } else {

   listBody.clear()

   if (list.size() < snakeBody) {

    for (int i = list.size() - 1 i > 0 i--) {

     listBody.add(list.get(i))

    }

   } else {

    for (int i = list.size() - 1 listBody.size() < snakeBody i--) {

     listBody.add(list.get(i))

    }

   }

  }

 }

 public boolean makeOut() {

  if ((x < 3 || y < 3) || (x > 305 || y > 305)) {

   return false

  }

  for (int i = 0 i < listBody.size() - 1 i++) {

   for (int j = i + 1 j < listBody.size() j++) {

    if (listBody.get(i).equals(listBody.get(j))) {

     return false

    }

   }

  }

  return true

 }

}

贪吃蛇游戏 望采纳

import java.awt.Button

import java.awt.Color

import java.awt.GridLayout

import java.awt.Point

import java.awt.event.KeyEvent

import java.awt.event.KeyListener

import java.util.*

import javax.swing.JFrame

import javax.swing.JOptionPane

public class Snake extends JFrame implements KeyListener{

int Count=0

Button[][] grid = new Button[20][20]

ArrayList<Point>snake_list=new ArrayList<Point>()

Point bean=new Point(-1,-1)//保存随机豆子【坐标】

int Direction = 1//方向标志 1:上2:下 3:左 4:右

//构造方法

public Snake()

{

//窗体初始化

this.setBounds(400,300,390,395)

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

GridLayout f=new GridLayout(20,20)

this.getContentPane().setBackground(Color.gray)

this.setLayout(f)

//初始化20*20个按钮

for(int i=0i<20i++)

for(int j=0j<20j++)

{

grid[i][j]=new Button()

this.add(grid[i][j])

grid[i][j].setVisible(false)

grid[i][j].addKeyListener(this)

grid[i][j].setBackground(Color.blue)

}

//蛇体初始化

grid[10][10].setVisible(true)

grid[11][10].setVisible(true)

grid[12][10].setVisible(true)

grid[13][10].setVisible(true)

grid[14][10].setVisible(true)

//在动态数组中保存蛇体按钮坐毁歼蔽标【行列】信息

snake_list.add(new Point(10,10))

snake_list.add(new Point(11,10))

snake_list.add(new Point(12,10))

snake_list.add(new Point(13,10))

snake_list.add(new Point(14,10))

this.rand_bean()

this.setTitle("总分:0")

this.setVisible(true)

}

//该方法随机一个豆子,且不在蛇体上,并使豆子可见

public void rand_bean(){

Random rd=new Random()

do{

bean.x=rd.nextInt(20)//纤州行

bean.y=rd.nextInt(20)//列

}while(snake_list.contains(bean))

grid[bean.x][bean.y].setVisible(true)

grid[bean.x][bean.y].setBackground(Color.red)

}

//判断拟增蛇头是否与自身有碰撞改含

public boolean is_cross(Point p){

boolean Flag=false

for(int i=0i<snake_list.size()i++){

if(p.equals(snake_list.get(i) )){

Flag=truebreak

}

}

return Flag

}

//判断蛇即将前进位置是否有豆子,有返回true,无返回false

public boolean isHaveBean(){

boolean Flag=false

int x=snake_list.get(0).x

int y=snake_list.get(0).y

Point p=null

if(Direction==1)p=new Point(x-1,y)

if(Direction==2)p=new Point(x+1,y)

if(Direction==3)p=new Point(x,y-1)

if(Direction==4)p=new Point(x,y+1)

if(bean.equals(p))Flag=true

return Flag

}

//前进一格

public void snake_move(){

if(isHaveBean()==true){//////////////有豆子吃

Point p=new Point(bean.x,bean.y)//【很重要,保证吃掉的是豆子的复制对象】

snake_list.add(0,p)//吃豆子

grid[p.x][p.y].setBackground(Color.blue)

this.Count++

this.setTitle("总分:"+Count)

this.rand_bean() //再产生一个豆子

}else{///////////////////无豆子吃

//取原蛇头坐标

int x=snake_list.get(0).x

int y=snake_list.get(0).y

//根据蛇头坐标推算出拟新增蛇头坐标

Point p=null

if(Direction==1)p=new Point(x-1,y)//计算出向上的新坐标

if(Direction==2)p=new Point(x+1,y)//计算出向下的新坐标

if(Direction==3)p=new Point(x,y-1)//计算出向左的新坐标

if(Direction==4)p=new Point(x,y+1)//计算出向右的新坐标

//若拟新增蛇头碰壁,或缠绕则游戏结束

if(p.x<0||p.x>19|| p.y<0||p.y>19||is_cross(p)==true){

JOptionPane.showMessageDialog(null, "游戏结束!")

System.exit(0)

}

//向蛇体增加新的蛇头坐标,并使新蛇头可见

snake_list.add(0,p)

grid[p.x][p.y].setVisible(true)

//删除原蛇尾坐标,使蛇尾不可见

int x1=snake_list.get(snake_list.size()-1).x

int y1=snake_list.get(snake_list.size()-1).y

grid[x1][y1].setVisible(false)

snake_list.remove(snake_list.size()-1)

}

}

@Override

public void keyPressed(KeyEvent e) {

if(e.getKeyCode()==KeyEvent.VK_UP &&Direction!=2) Direction=1

if(e.getKeyCode()==KeyEvent.VK_DOWN &&Direction!=1) Direction=2

if(e.getKeyCode()==KeyEvent.VK_LEFT &&Direction!=4) Direction=3

if(e.getKeyCode()==KeyEvent.VK_RIGHT &&Direction!=3) Direction=4

}

@Override

public void keyReleased(KeyEvent e) { }

@Override

public void keyTyped(KeyEvent e) { }

public static void main(String[] args) throws InterruptedException {

Snake win=new Snake()

while(true){

win.snake_move()

Thread.sleep(300)

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/yw/12462790.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存