1 package chapter8对象和类;
 2 
 3 public class Circle2 {
 4     double radius;
 5     static int numberOfObjects = 0;
 6 
 7     Circle2() {
 8         radius = 1.0;
 9         numberOfObjects++;
10     }
11 
12     Circle2(double newRadius) {
13         radius = newRadius;
14         numberOfObjects++;
15     }
16 
17     static int getNumberOfObject() {
18         return numberOfObjects;
19     }
20 
21     double getArea() {
22         return radius * radius * Math.PI;
23     }
24 
25 }
package chapter8对象和类;

public class Circle2 {
    double radius;
    static int numberOfObjects = 0;

    Circle2() {
        radius = 1.0;
        numberOfObjects++;
    }

    Circle2(double newRadius) {
        radius = newRadius;
        numberOfObjects++;
    }

    static int getNumberOfObject() {
        return numberOfObjects;
    }

    double getArea() {
        return radius * radius * Math.PI;
    }

}
package chapter8对象和类;

import javax.swing.*;

public class GUIComponents {
    public static void main(String[] args) {
        JButton jbtOK = new JButton("Ok");
        JButton jbtCancel = new JButton("Cancel");
        JLabel jlbName = new JLabel("Enter your name: ");
        JTextField jtfName = new JTextField("Type Name Here");
        JCheckBox jchkBold = new JCheckBox("Bold");
        JCheckBox jchkItalic = new JCheckBox("Italic");
        JRadioButton jrbRed = new JRadioButton("Red");
        JRadioButton jrbyellow = new JRadioButton("Yellow");
        JComboBox jcboColor = new JComboBox(new String[] { "Freshman",
                "Sophomore", "Junior", "Senior" });

        JPanel panel = new JPanel();
        panel.add(jbtOK);
        panel.add(jbtCancel);
        panel.add(jlbName);
        panel.add(jtfName);
        panel.add(jchkBold);
        panel.add(jchkItalic);
        panel.add(jrbRed);
        panel.add(jrbyellow);
        panel.add(jcboColor);

        JFrame frame = new JFrame();
        frame.add(panel);
        frame.setTitle("Show GUI Components");
        frame.setSize(450, 100);
        frame.setLocation(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}
package chapter8对象和类;

public class TestCircle1 {
    public static void main(String[] args) {
        
        Circle1 myCircle = new Circle1(5.0);
        System.out.println("The area of the circle of radius "
                + myCircle.radius + " is " + myCircle.getArea());

        Circle1 yourCircle = new Circle1();
        System.out.println("The area of the circle of radius "
                + yourCircle.radius + " is " + yourCircle.getArea());

        yourCircle.radius = 100;
        System.out.println("The area of the circe of radius "
                + yourCircle.radius + " is " + yourCircle.getArea());
    }
}

class Circle1 {
    double radius;

    Circle1() {
        radius = 1.0;
    }

    Circle1(double newRadius) {
        radius = newRadius;
    }

    double getArea() {
        return radius * radius * Math.PI;
    }
}
package chapter8对象和类;

public class TestCircle2 {
    @SuppressWarnings("static-access")
    public static void main(String[] args) {
        System.out.println("Before creating objects");
        System.out.println("The number of Circle objects is "
                + Circle2.numberOfObjects);

        Circle2 c1 = new Circle2();

        System.out.println("\nAfter creating c1");
        System.out
                .println("c1: radius(" + c1.radius
                        + ") and number of Circle objects ("
                        + c1.numberOfObjects + ")");

        Circle2 c2 = new Circle2(5);
        c1.radius = 9;

        System.out.println("\nAfter creating c2 and modifying c1");
        System.out
                .println("c1: radius (" + c1.radius
                        + ") and number of Circle objects ("
                        + c1.numberOfObjects + ")");

        System.out
                .println("c2: radius (" + c2.radius
                        + ") and number of Circle objects ("
                        + c2.numberOfObjects + ")");
    }
}
package chapter8对象和类;

public class TestCircle3 {
    public static void main(String[] args) {
        Circle3 myCircle = new Circle3(5.0);
        System.out.println("The ares of the circle of radius "
                + myCircle.getRadius() + " is " + myCircle.getArea());
        
        myCircle.setRadius(myCircle.getRadius() * 1.1);
        
        System.out.println("The area of the circle of radius "
                + myCircle.getRadius() + " is " + myCircle.getArea());
        
        System.out.println(Circle3.getNumberOfObjects());
    }
}
package chapter8对象和类;

import javax.swing.*;

public class TestFrame {
    public static void main(String[] args) {
        JFrame frame1 = new JFrame();
        frame1.setTitle("Window 1");
        frame1.setSize(200, 150);
        frame1.setLocation(200, 100);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setVisible(true);

        JFrame frame2 = new JFrame();
        frame2.setTitle("Window 2");
        frame2.setSize(200, 150);
        frame2.setLocation(410, 100);
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2.setVisible(true);

    }
}
package chapter8对象和类;

public class TestTV {
    public static void main(String[] args) {
        TV tv1 = new TV();
        tv1.turnOn();
        tv1.setChannel(30);
        tv1.setvolume(3);

        TV tv2 = new TV();
        tv2.turnOn();
        tv2.channelUp();
        tv2.channelUp();
        tv2.volumeUp();

        System.out.println("tv1's channel is " + tv1.channel
                + " and volume level is " + tv1.volumeLevel);
        System.out.println("tv2's channel is " + tv2.channel
                + " and volume level is " + tv2.volumeLevel);
    }

}
package chapter8对象和类;

public class TotalArea {
    public static void main(String[] args) {
        Circle3[] circleArray;

        circleArray = createCircleArray();

        printCircleArray(circleArray);
    }

    public static Circle3[] createCircleArray() {
        Circle3[] circleArray = new Circle3[5];

        for (int i = 0; i < circleArray.length; i++) {
            circleArray[i] = new Circle3(Math.random() * 100);
        }
        return circleArray;
    }

    public static void printCircleArray(Circle3[] circleArray) {
        System.out.printf("%-30s%-15s\n", "Radius", "Area");

        for (int i = 0; i < circleArray.length; i++) {
            System.out.printf("%-30f%-15f\n", circleArray[i].getRadius(),
                    circleArray[i].getArea());
        }
        System.out.println("-------------------------------------------");
        System.out.printf("%-30s%-15f\n", "The total areas of circles is",
                sum(circleArray)); // s-字符串;f-float型。
    }

    public static double sum(Circle3[] circleArray) {
        double sum = 0;

        for (int i = 0; i < circleArray.length; i++)
            sum += circleArray[i].getArea();

        return sum;
    }
}
package chapter8对象和类;

public class TV {
    int channel = 1;
    int volumeLevel = 1;
    boolean on = false;

    public TV() {
    }

    public void turnOn() {
        on = true;
    }

    public void turnOff() {
        on = false;
    }

    public void setChannel(int newChannel) {
        if (on && newChannel >= 1 && newChannel <= 120)
            channel = newChannel;
    }

    public void setvolume(int newVolumeLevel) {
        if (on && newVolumeLevel >= 1 && newVolumeLevel <= 7)
            volumeLevel = newVolumeLevel;
    }

    public void channelUp() {
        if (on && channel < 120)
            channel++;
    }

    public void channelDown() {
        if (on && channel > 1)
            channel--;
    }

    public void volumeUp() {
        if (on && volumeLevel < 7)
            volumeLevel++;
    }

    public void volumeDown() {
        if (on && volumeLevel > 1)
            volumeLevel--;
    }
}

 

posted on 2013-02-14 21:56  bailun  阅读(865)  评论(0编辑  收藏  举报