Java ninth job: Chapter 9 contains three examples and chapter 9 of Chapter 9

Source: Internet
Author: User

Java ninth job: Chapter 9 contains three examples and chapter 9 of Chapter 9

Job 1:

Example 9.1 create a circle class and obtain the perimeter and area based on the circle radius

Package com. swift; // abstract method composition class, which encapsulates attributes and methods in public class Circle {// two aspects: one is the field also called attribute, and the other is the method, also known as the implemented function private double radius; // constructor, with parameters to construct public Circle (double radius) {this. radius = radius;} // method overload. Different parameters // constructor. public Circle () {this. radius = 1;} // Method for Calculating the circular area public double getArea () {return radius * Math. PI;} // Method for Calculating the circumference public double getPerimeter () {return 2 * Math. PI * radius;} public void setRadius (double newRadius) {this. radius = newRadius ;}}

Circle display

package com.swift;public class DemoCircle {    public static void main(String[] args) {        Circle circle1=new Circle();        double area=circle1.getArea();        System.out.println(area);        Circle circle2=new Circle(10);        System.out.println(circle2.getArea());        System.out.println(circle1.getPerimeter());        System.out.println(circle2.getPerimeter());        double ridius=10;        double areaCircle=Math.PI*ridius*ridius;        System.out.println(areaCircle);        circle2.setRadius(5);        System.out.println(circle2.getArea());    }}

Job 2:

Combine the classes of the above two circles into one class and implement the same function.

package com.swift;public class SimpleCircle {    private double radius;    public SimpleCircle() {        this.radius=1;    }    public SimpleCircle(double radius){        this.radius=radius;    }    public double getArea() {        // TODO Auto-generated method stub        return Math.PI*radius*radius;    }    public double getPerimeter() {        return 2*Math.PI*radius;    }        public static void main(String[] args) {        SimpleCircle cir1=new SimpleCircle();        System.out.println("The area of the circle of radius "+cir1.radius+" is "+cir1.getArea());        SimpleCircle cir2=new SimpleCircle(10);        System.out.println("The area of the circle of radius "+cir2.radius+" is "+cir2.getArea());    }}

Job 3:

Example 9.3 create a TV set and implement the functions of FM channels and sound adjustment

Package com. swift; public class TV {public int channel = 1; public int volumeLevel = 1; public boolean on = false; public TV () {} public void turnOn () {on = true; system. out. println ("the TV has been turned on. ");} Public void turnOff () {on = false; System. out. println (" the TV has been disabled. ");} Public int getChannel () {return channel;} public void setChannel (int channel) {if (on) {System. out. println ("the TV is on. You can start to set up the TV. "); If (channel> = 1 & channel <= 120) {this. channel = channel; System. out. println ("channel has been adjusted to" + channel + "");} else {System. out. println ("the channel you want to call does not exist. ") ;}} Else {System. out. println ("the TV cannot be redeployed when it is turned off") ;}} public int getVolumeLevel () {return volumeLevel;} public void setVolumeLevel (int volumeLevel) {if (on) {System. out. println ("TV on, adjustable sound size"); if (volumeLevel> = 1 & volumeLevel <= 7) {this. volumeLevel = volumeLevel; System. out. println ("the sound size is set to" + volumeLevel + "size") ;}} else {System. out. println ("audio cannot be played when the TV is turned off") ;}} 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 --;}}}

Test TV class

package com.swift;public class TestTV {    public static void main(String[] args) {//        TV tv1=new TV();//        tv1.turnOff();//        tv1.setChannel(30);//        tv1.setVolumeLevel(3);                TV tv2=new TV();        tv2.turnOn();        System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);        tv2.channelUp();        System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);        tv2.channelUp();        System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);        tv2.channelUp();        System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);        tv2.volumeUp();        System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);        tv2.volumeUp();        System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);        tv2.volumeUp();        System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);                    }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.