例4-1
public class ComputerCircleArea{
public static void main(String args[])
{
double radius;
double area;
radius=163.16;
area=3.14*radius*radius;
System.out.printf("半径是%5.3f的圆的面积是:\n%5.3f\n",radius,area);
}
}
![](https://img-my.csdn.net/uploads/201303/17/1363504873_8195.jpg)
例4-2
class Circle{
double radius;
double getArea(){
double area=3.14*radius*radius;
return area;
}
}
public class Example4_2{
public static void main(String args[]){
Circle circle;
circle=new Circle();
circle.radius=163.16;
double area=circle.getArea();
System.out.printf("半径是%5.3f的圆的面积:%5.3f\n",circle.radius,area);
}
}
![](https://img-my.csdn.net/uploads/201303/17/1363505407_3523.jpg)
例4-3
class Lader{
double above,bottom,height;
Lader(){}
Lader(double a, double b,double h)
{
above=a;
bottom=b;
height=h;
}
public void setAbove(double a)
{
above=a;
}
public void setBottom(double b)
{
bottom=b;
}
public void setHeight(double h)
{
height=h;
}
double computeArea(){
return(above+bottom)*height/2.0;
}
}
public class Example4_1{
public static void main(String args[]){
double a1=0,a2=0;
Lader laderOne,laderTwo;
laderOne=new Lader();
laderTwo=new Lader(10,88,20);
laderOne.setAbove(16);
laderOne.setBottom(26);
laderOne.setHeight(100);
laderTwo.setAbove(300);
laderTwo.setBottom(500);
a1=laderOne.computeArea();
a2=laderTwo.computeArea();
System.out.println("laderOne的above,bottom和height:"+laderOne.above+","+laderOne.bottom+","+laderOne.height);
System.out.println("laderOne的面积是"+a1);
System.out.println("laderTwo的above,bottom和height:"+laderTwo.above+","+laderTwo.bottom+","+laderTwo.height);
System.out.println("laderTwo的面积是"+a2);
}
}
![](https://img-my.csdn.net/uploads/201303/17/1363519888_6618.jpg)
例4-4
class Lader{
double above,height;
static double bottom;
void setAbove(double a){
above=a;
}
void setBottom(double b){
bottom=b;
}
double getAbove(){
return above;
}
double getBottom(){
return bottom;
}
}
class Example4_4{
public static void main(String args[]){
Lader.bottom=60;
Lader laderOne,laderTwo;
System.out.println("现在所有Lader对象的bottom全都是"+Lader.bottom);
laderOne=new Lader();
laderTwo=new Lader();
System.out.println("laderOne的bottom:"+laderOne.getBottom());
System.out.println("laderTwo的bottom:"+laderTwo.getBottom());
laderOne.setAbove(11);
laderTwo.setAbove(22);
laderTwo.setBottom(100);
System.out.println("现在所有Lader对象的bottom全都是"+Lader.bottom);
System.out.println("laderOne的above:"+laderOne.getAbove());
System.out.println("laderTwo的above:"+laderTwo.getAbove());
}
}
![](https://img-my.csdn.net/uploads/201303/17/1363520065_1965.jpg)
例4-5
class Tom{
final int MAX=100;
static final int MIN=20;
}
public class Example4_5{
public static void main(String args[]){
System.out.println(Tom.MIN);
Tom cat=new Tom();
int x=0;
x=Tom.MIN+cat.MAX;
System.out.println(x);
}
}
![](https://img-my.csdn.net/uploads/201303/17/1363520208_7712.jpg)
例4-6
class Computer{
double x,y;
static double max(double a,double b){
return a>b?a:b;
}
}
class Example4_6{
public static void main(String args[]){
double max=Computer.max(12,45);
System.out.println(max);
}
}
![](https://img-my.csdn.net/uploads/201303/17/1363520297_3182.jpg)
例4-7
class Tom{
void f(int x,double y){
x=x+1;
y=y+1;
System.out.printf("参数x和y的值分别是:%d,%3.2f\n",x,y);
}
}
public class Example4_7{
public static void main(String args[]){
int x=10;
double y=12.58;
Tom cat=new Tom();
cat.f(x,y);
System.out.printf("main 方法中 x和y的值仍然分别是:%d,%3.2f\n",x,y);
}
}
![](https://img-my.csdn.net/uploads/201303/17/1363520443_1062.jpg)
例4-8
Jerry(int n){
leg=n;
}
void setLeg(int n){
leg=n;
}
int getLeg(){
return leg;
}
}
class Tom
{
void f(Jerry mouse){
mouse.setLeg(12);
System.out.println("在执行方法f时,参数mouse修改了自己的leg的值");
System.out.println("当前参数mouse的成员leg的值:"+mouse.getLeg());
mouse=null;
}
}
public class Example4_8{
public static void main(String args[]){
Tom cat=new Tom();
Jerry jerry=new Jerry(2);
System.out.println("在调用方法f之前,jerry的成员leg的值:"+jerry.getLeg());
cat.f(jerry);
System.out.println("在调用方法f之后,jerry的成员leg的值:"+jerry.getLeg());
}
}
![](https://img-my.csdn.net/uploads/201303/17/1363520556_8374.jpg)
例4-9
class Circle{
double radius;
Circle(double r){
radius=r;
}
double computerArea(){
return 3.14*radius*radius;
}
void setRadius(double newRadius){
radius=newRadius;
}
double getRadius(){
return radius;
}
}
class Cone{
Circle bottom;
double height;
Cone(Circle c,double h){
bottom=c;
height=h;
}
double computerVolume(){
double volume;
volume=bottom.computerArea()*height/3.0;
return volume;
}
void setBottomRadius(double r){
bottom.setRadius(r);
}
double getBottomRadius(){
return bottom.getRadius();
}
}
public class Example4_9{
public static void main(String args[]){
Circle circle=new Circle(8);
Cone circular=new Cone(circle,18);
System.out.println("circle的bottom半径:"+circular.getBottomRadius());
System.out.println("circle的体积:"+circular.computerVolume());
circular.setBottomRadius(88);
System.out.println("circular的bottom半径:"+circular.getBottomRadius());
System.out.println("circular的体积:"+circular.computerVolume());
circular.setBottomRadius(88);
System.out.println("circular的bottom半径:"+circular.getBottomRadius());
System.out.println("circular的体积:"+circular.computerVolume());
}
}
![](https://img-my.csdn.net/uploads/201303/17/1363520790_6500.jpg)
例4-10
class People{
double getArea(double x,int y){
return x*y;
}
int getArea(int x,double y){
return (int)(x*y);
}
double getArea(float x,float y,float z){
return(x*x+y*y+z*z)*2.0;
}
}
public class Example4_10{
public static void main(String args[]){
People zhang=new People();
System.out.println("面积:"+zhang.getArea(10,3.88));
System.out.println("面积:"+zhang.getArea(10.0,8));
}
}
![](https://img-my.csdn.net/uploads/201303/17/1363520885_4212.jpg)
例4-11
public class Tom{
int leg;
Tom(int n){
this.cry();
leg=n;
this.cry();
}
void cry(){
System.out.println("我是Tom,我现在有"+leg+"条腿");
}
public static void main(String args[]){
Tom cat=new Tom(4);
}
}
![](https://img-my.csdn.net/uploads/201303/17/1363521091_3967.jpg)
例4-12
package tom.jiafei;
class Tom{
void speak(){
System.out.println("Tom类在tom.jiafei包中");
}
}
public class Example4_12{
public static void main(String args[]){
Tom cat=new Tom();
cat.speak();
System.out.println("Example4_12类在tom.jiafei包中");
}
}
package tom.jiafei;
class Tom{
void speak(){
System.out.println("Tom类在tom.jiafei包中");
}
}
public class Example4_12{
public static void main(String args[]){
Tom cat=new Tom();
cat.speak();
System.out.println("Example4_12类在tom.jiafei包中");
}
}
![](https://img-my.csdn.net/uploads/201303/17/1363522012_9780.jpg)
例4-13
import java.util.Date;
public class Example4_13{
public static void main(String args[]){
Date date=new Date();
System.out.printf("本机机器的时间:\n%s",date);
}
}
![](https://img-my.csdn.net/uploads/201303/17/1363522211_3189.jpg)
例4-14
import tom.jiafei.*;
public class Example4_14{
public static void main(String args[]){
SquareEquation equation=new SquareEquation(4,5,1);
equation.getRoots();
equation.setCoefficient(-3,4,5);
equation.getRoots();
}
}
例4-15
public class Example4_15{
private int money;
Example4_15(){
money=2000;
}
private int getMoney(){
return money;
}
public static void main(String args[]){
Example4_15 exa=new Example4_15();
exa.money=3000;
int m=exa.getMoney();
System.out.println("money="+m);
}
}
![](https://img-my.csdn.net/uploads/201303/17/1363522508_8025.jpg)
例4-16
class Employee{
private double salary=1800;
public void setSalary(double salary){
if(salary>1800&&salary<=6000){
this.salary=salary;
}
}
public double getSalary(){
return salary;
}
}
public class Example4_16{
public static void main(String args[]){
Employee zhang=new Employee();
Employee wang=new Employee();
zhang.setSalary(100);
System.out.println("zhang的薪水:"+zhang.getSalary());
wang.setSalary(3888);
System.out.println("wang的薪水:"+wang.getSalary());
}
}
![](https://img-my.csdn.net/uploads/201303/17/1363522673_8407.jpg)
例4-17
Rectangle.java
public class Rectangle{
private double x,y,width,height;
public void setX(double x){
this.x=x;
}
public double getX(){
return x;
}
public void setY(double y){
this.y=y;
}
public double getY(){
return y;
}
public void setWidth(double width){
if(width<=0)
this.width=0;
else
this.width=width;
}
public double getWidth(){
return width;
}
public void setHeight(double height){
if(height<=0)
height=0;
else
this.height=height;
}
public double getHeight(){
return height;
}
}
Circle.java
public class Circle{
private double x,y,radius;
public void setX(double x){
this.x=x;
} public double getX(){
return x;
}
public void setY(double y){
this.y=y;
}
public double getY(){
return y;
}
public void setRadius(double radius){
if(radius=0)
this.radius=0;
else
this.radius=radius;
}
public double getRadius(){
return radius;
}
}
MainClass.java
public class MainClass{
public static void main(String args[]){
Rectangle rect1=new Rectangle(),rect2=new Recatangle();
Circle circle1=new Circle(),circle2=new Circle();
Geometry geometryOne,geometryTwo;
geometryOne=new Geometry(rect1,circle1);
geometryOne.setRectanglePosition(30,40);
geometryOne.setRectangleWidthAndHeight(120,80);
geometryOne.setCirclePosition(150,30);
geometryOne.setCircleRadius(60);
geometryTwo=new Geometry(rect2,circle2);
geometryTwo.setRectanglePosition(160,160);
geometryTwo.setRectangleWidthAndHeight(120,80);
geometryTwo.setCirclePosition(40,30);
geometryTwo.setCircleRadius(60);
geometryOne.showState();
geometryTwo.showState();
}
}
例4-18
public class Example4_18{
public static void main(String args[]){
char a[]={'a','b','c','D','E','F'};
for(int i=0;i<a.length;i++){
if(Character.isLowerCase(a[i])){
a[i]=Character.toLowerCase(a[i]);
}
else if(Character.isUpperCase(a[i])){
a[i]=Character.toLowerCase(a[i]);
}
}
for(int i=0;i<a.length;i++){
System.out.printf("%6c",a[i]);
}
}
}
![](https://img-my.csdn.net/uploads/201303/17/1363522897_1472.jpg)
例4-19
public class Example4_19{
public static void main(String args[]){
Integer m[]=new Integer[10];
for(int i=0;i<m.length;i++){
m[i]=new Integer(101+i);
}
for(int i=0;i<m.length;i++){
System.out.println(m[i].intValue());
}
}
}
![](https://img-my.csdn.net/uploads/201303/17/1363523154_2442.jpg)