编程题代码,仅供参考

  第一周 类与对象

  分数

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Fraction a = new Fraction(in.nextInt(), in.nextInt());
        Fraction b = new Fraction(in.nextInt(),in.nextInt());
        a.print();
        b.print();
        a.plus(b).print();
        a.multiply(b).plus(new Fraction(5,6)).print();
        a.print();
        b.print();
        in.close();
    }
}
//new对象的时候调用构造
class Fraction
{
    int a,b;
    //构造
    Fraction(int a, int b)
    {
        this.a = a;
        this.b = b;
        adb();//约分
    }
    //toDouble
    double toDouble()
    {
        double d = (double)a/b;
        return d;
    }
    //plus
    Fraction plus(Fraction r)
    {
        int n = this.a*r.b +r.a*this.b;
        int d = this.b * r.b;
        return new Fraction(n,d);
    }
    //multiply
    Fraction multiply(Fraction r)
    {    
        int n = this.a*r.a;
        int d = this.b*r.b;
        return new Fraction(n,d);
    }
    //print
    void print()
    {    
        if(a==b)
            System.out.println(1);
        else 
            System.out.println(a+"/"+b);    
    }
    //约分函数
    private void adb ()
    {
        int i = a > b ? b : a;
        for (;i>1;i--)
        {
            if(a%i==0&&b%i==0)
            {
                a /= i;
                b /= i;
            }    
        }
    }
}
View Code

  第二周 对象交互

  有秒计时的数字时钟

public class Main {

    public static void main(String[] args) {
        java.util.Scanner in = new java.util.Scanner(System.in);
        Clock clock = new Clock(in.nextInt(), in.nextInt(), in.nextInt());
        clock.tick();
        System.out.println(clock);
        in.close();
    }
    
}


class Clock {

    private Display s = new Display(60);
    private Display m = new Display(60);
    private Display h = new Display(24);
    
    public Clock(int hour, int minute, int second)
    {
        s.setvalue(second);
        m.setvalue(minute);
        h.setvalue(hour);
    }
    
    public void tick()
    {
        
        if(s.increase())
        {
            if(m.increase())
            {
                h.increase();
            }
        }

    }
    
    public String toString()
    {
        String str = String.format("%02d:%02d:%02d",h.getvalue(),m.getvalue(),s.getvalue());
        
        return str;
    }

}

class Display {
    
    private int value = 0;
    private int limit = 0;
    
    public Display(int limit)
    {
        this.limit = limit;
    }
    
    public boolean increase()
    {
        boolean b = false;
        value++;
        if (value == limit)
        {
            value = 0;
            b = true;
        }
        return b;
    }

    public int getvalue()
    {    
        return value;
    }
    public void setvalue(int x)
    {
        value = x;
    }
}
View Code

  第三周 对象容器

  查找里程

import java.util.ArrayList;
import java.util.Scanner;
  
public class Main
{
    public static void main(String[] args)
    {
        ArrayList<String> city = new ArrayList<String>();
        Scanner in = new Scanner(System.in);
          
        while(true)
        {   
            String cityName = in.next();
              
            if(cityName.equals("###")){
                break;
            }

            city.add(cityName); 
        }   
          
        int [][] distance = new int [city.size()][city.size()];
          
        for(int i = 0; i < city.size(); i++){
            for(int j = 0; j < city.size(); j++){
                distance[i][j] = in.nextInt();    
            }
        }
          
        int i = city.indexOf(in.next());
        int j = city.indexOf(in.next());
          
        System.out.println(distance[i][j]); 
        in.close();
    }
}
View Code

  第四周 继承多态

  媒体类

package dome;
import java.util.ArrayList;
public class Database {
    private ArrayList<Item> listItem = new ArrayList<Item>();
    public void add(Item item){
        listItem.add(item);
    } 
    public void list(){
        for(Item item:listItem){
            item.print();
        }
    } 
    public static void main(String[] args) {
        Database db = new Database();
        db.add(new CD("I am a CD","Even",6,24,true,"good"));
        db.add(new DVD("I am a DVD","Even",20,true,"wonderful"));
        db.add(new Mp3("I am a Mp3","Even",20,true,"talented","Hello World"));
        db.list();
    }
}

package dome;
public class CD extends Item {
    private String artist;
    private int numofTrack;
    public CD(String title, String artist, int numofTrack, int playingTime,boolean gotIt,
    String comment) {
        super(title, playingTime, gotIt, comment);
        this.artist = artist;
        this.numofTrack = numofTrack;
    }
    public static void main(String[] args) {}
    public void print() {
        super.print();
        System.out.print("artist: "+artist+"   ");
        System.out.println("numofTrack: "+numofTrack);
    }
}

package dome;
public class DVD extends Item {
    private String director;
    public DVD(String title, String director, int playingTime, boolean gotIt ,String comment) {
        super(title, playingTime, gotIt , comment);
        this.director = director;
    }
    public static void main(String[] args) {
    }
    public void print() {
        super.print();
        System.out.println("director: "+director);
    }
}

package dome;
public class Mp3 extends Item{
    private String lyics;
    private String singer;
    public Mp3(String title, String singer, int playingTime, boolean gotIt, String comment,
            String lyics) {
        super(title, playingTime, gotIt, comment);
        this.lyics = lyics;
        this.singer = singer;
    }
    public static void main(String[] args) {
    }
    public void print() {
        super.print();
        System.out.print("singer: "+singer+"   ");
        System.out.println("lyics: "+lyics);
    }
}

package dome;
public class Item {
    private String title;
    private int playingTime;
    private boolean gotIt;
    private String comment;
    public Item(String title, int playingTime, boolean gotIt ,String comment) {
        super();
        this.title = title;
        this.playingTime = playingTime;
        this.gotIt = true;
        this.comment = comment;
    }
    public static void main(String[] args) {
    }
    public void print() {
        System.out.print("title: "+title+"   ");
        System.out.print("playingTime: "+playingTime+"   ");
        System.out.print("gotIt: "+gotIt+"   ");
        System.out.print("comment: "+comment+"   ");
    }
}
View Code

  课程相关代码

  1、Shapes

  抽象类Shape,抽象方法draw(参数为Graphics类对象)

package shapes;

import java.awt.Graphics;

//抽象类Shape
public abstract class Shape {
    
    public abstract void draw(Graphics g);//抽象方法 draw
    
}
View Code

  Shape的子类Rectangle,override Shape的draw方法

package shapes;

import java.awt.Graphics;

public class Rectangle extends Shape {
    private int x;
    private int y;
    private int width;
    private int height;

    public Rectangle(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    @Override
    public void draw(Graphics g) {
        g.drawRect(x, y, width, height);
    }

}
View Code

  Shape的子类Circle,override Shape的draw方法

package shapes;

import java.awt.Graphics;

public class Circle extends Shape {
    private int x;
    private int y;
    private int radius;
    
    public Circle(int x, int y, int radius)
    {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }
    @Override
    public void draw(Graphics g) {
        g.drawOval(x-radius, y-radius, radius*2, radius*2);
    }
}
View Code

  Shape的子类Line,override Shape的draw方法

package shapes;

import java.awt.Graphics;

public class Line extends Shape {
    private int x1;
    private int y1;
    private int x2;
    private int y2;
    
    public Line(int x1, int y1, int x2, int y2)
    {
        this.x1 = x1; this.y1 = y1;
        this.x2 = x2; this.y2 = y2;
    }
    
    @Override
    public void draw(Graphics g) {
        g.drawLine(x1, y1, x2, y2);
    }

}
View Code

  Shape的子类Triangle,override Shape的draw方法

package shapes;

import java.awt.Graphics;

public class Triangle extends Shape {
    private int[] x = new int[3];
    private int[] y = new int[3];
    
    public Triangle(int x1, int y1, int x2, int y2, int x3, int y3)
    {
        x[0] = x1; x[1] = x2; x[2] = x3;
        y[0] = y1; y[1] = y2; y[2] = y3;
    }
    
    @Override
    public void draw(Graphics g) {
        g.drawPolygon(x, y, x.length);
    }

}
View Code

  Picture类继承自JFrame类, 成员变量,高、宽、包含Shape对象的ArrayList容器,内部类ShapesPanel继承自Jpanel类,该类重写了paintComponent方法,draw Shape对象

package shapes;

import java.awt.Graphics;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Picture extends JFrame {
    private static final long serialVersionUID = 1L;
    private int width;
    private int height;
    
    private ArrayList<Shape> listShape = new ArrayList<Shape>();
    
    private class ShapesPanel extends JPanel {
        private static final long serialVersionUID = 1L;

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            for ( Shape s : listShape )
            {
                s.draw(g);
            }            
        }
        
    }
    
    public void add(Shape s)
    {
        listShape.add(s);
    }

    public Picture(int width, int height)
    {
        add(new ShapesPanel());
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.width = width;
        this.height = height;
    }
    
    public void draw()
    {
        setLocationRelativeTo(null);
        setSize(width, height);
        setVisible(true);
    }
}
View Code

  MyPic为测试类,测试绘制JFrame Jpanel,并在窗体上绘制Shape对象

package shapes;

public class MyPic {
    public static void main(String[] args) 
    {
        Picture pic = new Picture(420,300);
        Circle c1 = new Circle(320,40,80);
        Rectangle r1 = new Rectangle(100, 100, 100, 100);
        Triangle t1 = new Triangle(100, 100, 200, 100, 150, 50);
        Line l1 = new Line(0,205,400,205);
        Circle c2 = new Circle(200,200,50);
        pic.add(c1);
        pic.add(r1);
        pic.add(t1);
        pic.add(l1);
        pic.add(c2);
        pic.draw();    
    }
}
View Code

  相关知识:

  Swing 是一个为Java设计的GUI工具包,是JAVA基础类的一部分,包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表

  Graphics类提供基本的几何图形绘制方法,主要有:画线段、画矩形、画圆、画带颜色的图形、画椭圆、画圆弧、画多边形等

  理解 JFrame JPanel:什么是 JFrame JPanel ,它们都是容器, JFrame是顶级容器,可以把 JPanel放进去,JPanel有两个方法,一个是paint,一个是getPreferredSize,继承自它的类,覆盖这两个方法,paint是绘画,getPreferredSize是大小,只有JFrame,运行后,大小为0,放进JPanel运行后,为getPreferredSize的大小(JFrame要调用pack())

import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

//测试面板类TestJPanel
class TestJPanel extends JPanel{
    
    //绘画override
    @Override
    public void paint(Graphics g) {
        super.paint(g);
    }

    //绘画尺寸override
    @Override
    public Dimension getPreferredSize() {
        //参数修改,改变JPanel的大小
        return new Dimension(150,150);
    }
    
}

//测试类(空)
public class Test {
    
    public static void main(String[] args){
        
        //new JPanel
        TestJPanel testJPanel = new TestJPanel();
        
        //创建窗框(带参数直接加标题)
        JFrame frame = new JFrame("Test");
        
        //窗框标题
        //frame.setTitle("Hello");
        
        //关闭按钮程序关闭
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //窗框可否变化
        frame.setResizable(true);
                
        // JPanel 放入窗框(绝对布局)
        frame.add(testJPanel);
        testJPanel.setLayout(null);
        
        //按钮,放到JPanel
        JButton btnNewButton = new JButton("按钮");
        btnNewButton.setBounds(29, 55, 93, 23);
        testJPanel.add(btnNewButton);
        
        //关联getPreferredSize()
        frame.pack();
        
        //显示窗体
        frame.setVisible(true);
    }
}
View Code