7th jobs - access to objects using

Problem 1: 5 on the basis of the job, and then creating a column type, comprising a rectangle, and the high volume of the three member variables, a constructor method to initialize member variables, and calculating the volume, for the bottom two functions method, main category input length, width, height, calculated cylinder volume, enter a new length, width, height, creating a new rectangle, the method using soled soled, the cylinder volume is calculated again.

jx代码(4个成员变量,一个方法,两个访问器,两个修改器)

     package md;
   import java.util.Scanner;
           public class jx {
          static int chang;                                                                                      //定义四个成员变量 长  ,宽,面积,周长     
          static int kuan;
          static int area;
          static int zc;
     public int getChang(){                                                                                          //访问器
        return chang; 
     }
     public void setChang(int chang){                                                                              //修改器
         this.chang=chang;
     }
     public int getKuan(){                                                                                             //访问器
        return kuan; 
      }
      public void setKuan(int kuan){                                                                                    //修改器
         this.kuan=kuan;
      }
   static int getArea(){                                                                                                     //求面积的方法
          area=chang*kuan;
          return area;
      }

}

            zt代码(三个成员变量,一个访问器,一个修改器,一个主方法)

  package md;
   import md.jx; 
 import java.util.Scanner;

ZT class {public
static JX A; // member variables three
static int height;
static int V;
public int getHeight () {// Accessors
return height;

 }
 public void setHeight(){                                                                                                //修改器
     this.height=height;
 }
 public zt(jx a,int height,int v){   
     this.height=height;
     v=a.area*height; 
 }
public static void main(String[] args) {                                                                      //主方法
    Scanner reader=new Scanner (System.in);
   System.out.println("请输入长宽高");                                                                      //输入长宽高
   height=reader.nextInt();
   jx.chang=reader.nextInt();
   jx.kuan=reader.nextInt();
   v=a.getArea()*height;
   System.out.println("体积是"+v);                                                                             // 输出体积
   System.out.println("修改     请输入新的长宽高");                                                    //重新输入长宽高
   height=reader.nextInt();
   jx.chang=reader.nextInt();
   jx.kuan=reader.nextInt();
   v=a.getArea()*height;
   System.out.println("新的体积是"+v);                                                                     //输出新的体积
}

}

operation result

Problem 2: Design class called MyInteger, comprising: int type value a data field construction method, when the specified int value, object data created MyInteger field value and the access modifier isEven () and isOdd () method, if the current object is even or odd, class method returns true isPrime (MyInteger i), determines whether the specified value is a prime number, returns true MyInteger create objects in the main class, each class MyInteger verification method.

The code (a type comprising: a constructor, access, modify, a method of determining three)

   import java.util.Scanner;

public class Mylnteger {// Create a class Mylnteger
Private static Final Mylnteger Mylnteger = null;
static int value;
public Mylnteger (int value) {// Constructors
Mylnteger.value = value;
}
public int the getValue () {// Accessors
return value;

}
Public void the setValue () {// modifier
this.value = value;
}
public static Boolean isEven () {// determines whether the method is an odd
IF (value = 0!)
Return to true;
the else
return to false;

}
public static boolean isOdd () {// determines whether the method is even
IF (value% 2 == 0)
return to true;
the else
return to false;}

  public static boolean isPrime(Mylnteger i){                                                                          //判断是否是素数的类方法
      
       for(int j=2;j<i.value/2;j++){
           if(i.value%j==0)
               return false;
           break;
       }
    return true ;
    
    
   }
   

public static void main(String[] args) {
    Scanner reader=new Scanner(System.in);                                                             //引入scanner
       Mylnteger dd;                                                                                                  //创建对象
       dd=new Mylnteger(value);
       System.out.println("请输入一个数");
       dd.value=reader.nextInt();                                                                                //输入数进行判断
       System.out.println(dd.value+"是否为偶数"+isOdd());
       System.out.println(dd.value+"是否为奇数"+isEven());
       System.out.println(dd.value+"是否为素数"+isPrime(dd));
       System.out.println("请输入一个数");
       dd.value=reader.nextInt();                                                                                    //输入数进行判断
       System.out.println(dd.value+"是否为偶数"+isOdd());
       System.out.println(dd.value+"是否为奇数"+isEven());
       System.out.println(dd.value+"是否为素数"+isPrime(dd));
}

}

operation result

Guess you like

Origin www.cnblogs.com/12yy/p/11567617.html
7th
7th