top of page

<Java> Object Type Usage

  • 작성자 사진: Jeemin Myung
    Jeemin Myung
  • 2020년 11월 29일
  • 2분 분량

public class Tablet{

private String name;

private int price;

private double displaySize;

private int capacity;

private boolean cellular;

private String color;

private int serialNum;

private int appCount;


private static Tablet[] tablets = new Tablet[20];

private static int totalNumApps = 0;

private static int serialStart = -1;

private static int tabletCount = 0;


public Tablet(String name, int price, double displaySize, int capacity, boolean cellular, String color){

this.name=name;

this.price=price;

this.displaySize=displaySize;

this.capacity=capacity;

this.cellular=cellular;

this.color=color;

serialNum=getSerialNum();

}

public void installApp(){

totalNumApps +=1;

appCount +=1;

}

public static int getTotalNumberOfApps(){

return totalNumApps;

}

public int getAppCount(){

return appCount;

}

public int getSerialNum(){

serialStart+=1;

return serialStart;

}

public static void remove(Tablet tab){

for(int i=0;i<tabletCount;i++){

if(tablets[i].equals(tab)){

tablets[i]=null;

for(int j=i;j<tabletCount;j++){

Tablet temp = tablets[j];

tablets[j]=tablets[j+1];

tablets[j+1]=temp;

}

break;

}

}

totalNumApps=totalNumApps-tab.getAppCount();

tabletCount-=1;

}

public static void add(Tablet tab){

tablets[tabletCount]=tab;

tabletCount+=1;

}

public String toString(){

return " ("+name+", "+serialNum+", "+price+", "+displaySize+", "+capacity+", "+appCount+", "+cellular+", "+color+")";

}

public static void print(String s){

System.out.println(s);

for(int i=0;i<tabletCount;i++){

System.out.println(tablets[i]);

}

}

}

-------------------------------------------------------------------------------------------------

public class UseTablet {


public static void main (String[] args) {


Tablet tablet1 = new Tablet("Jim", 629, 9.7, 16, true, "red");

tablet1.installApp();

tablet1.installApp();

Tablet.add(tablet1);


Tablet tablet2 = new Tablet("Amy", 599, 9.7, 32, false, "space grey");

tablet2.installApp();

tablet2.installApp();

tablet2.installApp();

Tablet.add(tablet2);


Tablet tablet3 = new Tablet("Art", 829, 9.7, 64, true, "silver");

tablet3.installApp();

Tablet.add(tablet3);


Tablet tablet4 = new Tablet("Ken", 599, 9.7, 32, false, "pink");

tablet4.installApp();

tablet4.installApp();

tablet4.installApp();

tablet4.installApp();

Tablet.add(tablet4);


Tablet tablet5 = new Tablet("Jan", 829, 9.7, 64, true, "gold");

tablet5.installApp();

Tablet.add(tablet5);


Tablet.print("All Tablets:");

System.out.println("The total number of apps: " +

Tablet.getTotalNumberOfApps());


Tablet.remove(tablet3);

Tablet.print("All Tablets:");

System.out.println("The total number of apps: " +

Tablet.getTotalNumberOfApps());


Tablet.remove(tablet1);

Tablet.print("All Tablets:");

System.out.println("The total number of apps: " +

Tablet.getTotalNumberOfApps());


Tablet.remove(tablet5);

Tablet.print("All Tablets:");

System.out.println("The total number of apps: " +

Tablet.getTotalNumberOfApps());

}

}


 
 
 

최근 게시물

전체 보기
<Java> Inheritance Usage

public class Jet implements Comparable<Jet> { protected String manufacturer; protected String model; protected int year; protected String...

 
 
 

댓글


010-9127-2258

  • Facebook
  • Twitter
  • LinkedIn

©2020 by Jee Min Myung. Proudly created with Wix.com

bottom of page