输出圆的面积 java应用程序
publicclass ComputerCircleArea {
publicstaticvoid main(String ags[]){
double radius;
double area;
radius=163.16;
area=3.14*radius*radius;
System.out.println("半径是"+radius+"的圆的面积是:"+area);
}
}
使用Circle类创建circle对象,只需要让这个对象分别计算面积即可
class Circle{
doubleradius;
double getArea(){
double area=3.14*radius*radius;
return area;
}
}
publicclass Example {
publicstaticvoid main(String ags[]){
Circle circle;
circle=new Circle();
circle.radius=163.16;
double area=circle.getArea();
System.out.println("半径是"+circle.radius+"的圆的面积是:"+area);
}
}
在主类Example的main方法中使用Lader类创建两个对象laderOne和laderTwo
class Lader{
doubleabove,bottom,height;
Lader(){}
Lader(double a,double b,double c){
above=a;
bottom=b;
height=c;
}
publicvoid setAbove(double a){
above=a;
}
publicvoid setBottom(double b){
bottom=b;
}
publicvoid setHeight(double h){
height=h;
}
double computerArea(){
return (above+bottom)/2*height;
}
}
publicclass Example {
publicstaticvoid main(String ags[]){
Lader laderOne;
Lader laderTwo;
double area1=0,area2=0;
laderOne=new Lader();
area1=laderOne.computerArea();
System.out.println("laderOne的面积:"+area1);
laderTwo=new Lader(10,88,20);
area2=laderTwo.computerArea();
System.out.println("laderTow的面积:"+area2);
laderOne.setAbove(16);
laderOne.setBottom(26);
laderOne.setHeight(100);
laderTwo.setAbove(300);
laderTwo.setBottom(500);
System.out.println("laderOne的above,bottom,height:"+laderOne.above+","+laderOne.bottom+","+laderOne.height);
System.out.println("laderOne的面积是:"+laderOne.computerArea());
System.out.println("laderTow的above,bottom,height:"+laderTwo.above+","+laderTwo.bottom+","+laderTwo.height);
System.out.println("laderTow的面积是:"+laderTwo.computerArea());
}
}
两个Lader共享bottom
class Lader{
doubleabove,height;
static doublebottom;
void setAbove(double a){
above=a;
}
void setBottom(double b){
bottom=b;
}
double getAbove(){
returnabove;
}
double getBottom(){
returnbottom;
}
}
publicclass Example {
publicstaticvoid main(String ags[]){
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);
laderOne.setBottom(100);
System.out.println("现在所有Lader对象的bottom都是"+Lader.bottom);
System.out.println("laderOne的above:"+laderOne.getAbove());
System.out.println("laderTwo的above:"+laderTwo.getAbove());
System.out.println("laderOne的bottom:"+laderOne.getBottom());
System.out.println("laderTwo的bottom:"+laderTwo.getBottom());
}
}
常量的用法
class Tom{
finalintMAX=100;
staticfinalintMIN=20;
}
publicclass Example {
publicstaticvoid main(String ags[]){
System.out.println(Tom.MIN);
Tom cat=new Tom();
int x=0;
x=Tom.MIN+cat.MAX;
System.out.println(x);
}
}
通过类名调用类方法
class Computer{
doublex,y;
staticdouble max(double a,double b){
return a>b?a:b;
}
}
publicclass Example {
publicstaticvoid main(String ags[]){
double max=Computer.max(12,45);
System.out.println(max);
}
}
向一个方法的基本数据类型参数传值
class Tom{
void f(int x,double y){
x=x+1;
y=y+1;
System.out.println("参数x和y的值分别是:"+x+","+y);
}
}
publicclass Example {
publicstaticvoid main(String ags[]){
int x=10;
double y=12.58;
Tom cat=new Tom();
cat.f(x, y);
System.out.println("main方法中x和y的值仍然分别是:"+x+","+y);
}
}
Tom类中的方法f的参数mouse是Jerry类声明的对象,数组属于引用类型参数
class Jerry{
intleg;
Jerry(int n){
leg=n;
}
void setLeg(int n){
leg=n;
}
int getLeg(){
returnleg;
}
}
class Tom{
void f(Jerry mouse){
mouse.setLeg(12);
System.out.println("在执行方法f时,参数mouse修改了自己的leg的值");
System.out.println("当前参数mouse的成员leg的值:"+mouse.getLeg());
mouse=null;
//mouse.setLeg(12);
}
}
publicclass Example {
publicstaticvoid main(String ags[]){
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());
}
}
类Cone在创建对象时,将一个Circle对象的引用“传值”给Cone对象的bottom
class Circle{
doubleradius;
Circle(double r){
radius=r;
}
double computerArea(){
return 3.14*radius*radius;
}
void setRadius(double newRadius){
radius=newRadius;
}
double getRadius(){
returnradius;
}
}
class Cone{
Circle bottom;
doubleheight;
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(){
returnbottom.getRadius();
}
}
publicclass Example {
publicstaticvoid main(String ags[]){
Circle circle=new Circle(8);
Cone circular=new Cone(circle,18);
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());
}
}
类Area的getArea方法时重载方法
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;
}
}
publicclass Example {
publicstaticvoid main(String ags[]){
People zhang=new People();
System.out.println("面积:"+zhang.getArea(10, 3.88));
System.out.println("面积:"+zhang.getArea(10.0, 8));
}
}
使用java.util包中的Date类,用来显示本机的当前时间
import java.util.Date;
publicclass Example {
publicstaticvoid main(String ags[]){
Date date=new Date();
System.out.println("本地机器的时间:"+date);
}
}
publicclass Example{
privateintmoney;
Example(){
money=200;
}
privateint getMoney(){
returnmoney;
}
publicstaticvoid main(String ags[]){
Example exa=new Example();
exa.money=3000;
int m=exa.getMoney();
System.out.println("money="+m);
}
}
用某个类在另外一个类中创建对象后,如果不希望对改对象直接访问自己的变量,即通过“.”运算符来操作自己的成员变量,就应当将该成员变量访问权限设置为private。面向对象编程提倡对象应当调用方法来改变自己的属性,我们应当提供操作数据的方法,这些方法可以经过精心的设计,使得对数据的操作更加合理。
class Employee{
privatedoublesalary=1800;
publicvoid setSalary(double salary){
if(salary>1800&&salary<=6000){
this.salary=salary;
}
}
publicdouble getSalary(){
returnsalary;
}
}
publicclass Example{
publicstaticvoid main(String ags[]){
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());
}
}