简体   繁体   中英

i have confusion in concept of classes and objects in Java

i have confusion in concept of classes and objects in Java i did the following code but i'm not sure if it right!!! So How can i test it and invoking this class in the main class?

  import java.util.*;
  public class Pizza {
  public String pizzasize;
  public int cheese;
  public  int pepperoni;
  public Pizza (String pizzasize1,int cheese1,int pepperoni1){
  pizzasize= pizzasize1;
  cheese=cheese1;
  pepperoni=pepperoni1;
 }

this method chick pizza size then declaring and assign pizza cost

  public void setPizza(String pizzasize1){
     switch(pizzasize1)
  {
      case "S":
      case "s":   
      {
   int  pc=10;break;
      }
      case "M":
      case "m":   
      {
   int  pc=12;break;
      }
      case "L":
      case "l":   
      {
   int  pc=14;break;
      }
      default:System.out.print("Wrong");
  }//switch
   pizzasize= pizzasize1;
  }

..

 public void setPizza(int cheese1,int pepperoni1){
   cheese=cheese1;
  pepperoni=pepperoni1;
  }

 public String getPizza(){
 return  pizzasize;
 }
 public int getPizza1(){
 return  cheese;
 }
public int getPizza2(){
return  pepperoni;
}

public void calcCost (int pc){
    double totalCost = pc+(cheese+pepperoni) *2;       
}

finally this method for sample output

public void getDiscriptions(String pizzasize,int cheese,int pepperoni,double totalCost){
Scanner sc=new Scanner(System.in);
System.out.println("place order");
System.out.println("size: L,M,s");
pizzasize=sc.next();
System.out.println("cheese: ");
cheese=sc.nextInt();
System.out.println("pepperoni: ");
pepperoni=sc.nextInt();
System.out.println("");
System.out.println("your order placed is/nlarge pizza with"+cheese+"cheese,"
 +pepperoni+"pepperoni,/ntotal cost is"+totalCost);
}

}//

You've broken your example up into several chunks. It seems like all of those methods are supposed to be in the same Pizza class. Is that true?

The methods look like they belong in a command-line application. If that's the case, you're going to need a main(...) method.

class Pizza {

    ...the methods you want to test go here...

    public static void main(String[] args) {
        ...your top-level test code goes here...
    }
}

First you must compile it. If you have a command prompt, and you have the JDK installed, you can type this:

$ javac Pizza.java

Then, if the compiler gives no error messages, you can run it:

$ java Pizza

See the link provided by @PetterFriberg for more info.

If you want to run it in an Integrated Development Environment (IDE) such as Eclipse or IntelliJ, then that's a whole other question.

Suppose, you are Automobile Engineer and you get a contract to built new model car then what will you do to built car ??

I think, first you will gather information about:

New brand name , Size , Shape , Weight , Color etc.
Speed , Acceleration , Rotation etc.

After that you will start to design the blueprint of the car. The blueprint only shows how it works and how it looks. But you never able to feel it in realistic world with that blueprint.

To feel the car you have to built it with same feature as mentioned in blueprint. And only after that you can touch it, open the door, ride it, press brake and accelerator. With the same blueprint you can built car in any number as much as you want.

In OOP, Class is same as blueprint of car. It only shows you what car knows like: color , size , weight , height , speed and these are called attributes. It only tells you how car behaves like: it runs , it stops , it rotates , etc and these are called methods. Class doesn't exist in real world it's virtual.

class Car_Real {

    String brand_name;
    String color;
    float weight;
    float height;

    void runs() {
        System.out.println("Engine starts");
    }

    void accelerates() {    
        System.out.println("Speed goes on increasing");    
    }

    void brakes(){
        System.out.println("Speed goes on decreasing");    
    }

}

In OOP, Object is real car derived from blueprint.After you create object,you can access the door,you can ride it that means you can feel and access attributes and methods of class.

public class Car {

    public static void main(String[] args) {    
        Car_Real C1=new Car_Real(); //create object C1 from car    
        C1.runs();    
    }

}

And using same class you can create any number of objects as you want.

public class Car {

    public static void main(String[] args) {    
        Car_Real C1 = new Car_Real();    
        Car_Real C2 = new Car_Real();    
        C1.runs();    
        C2.brakes(); // create two object C1, C2    
    }

}

Not bad, but a few pointers:

switch statements don't work with Strings, they work with byte , short , char , and int primitive data types. They also work with enumerated types.

You could set up an enum with the sizes:

public enum Size {S,M,L};

Then your size field would be of type Size :

private Size pizzaSize;

Make your fields all private instead of public . That means they cannot be seen from outside the Pizza class. They can be read using getter methods. If they need to be changed, you can provide setter methods too.

Example:

public class Pizza {

  //cannot be accessed directly from other classes
  private int cheese;

  //allows other classes to read the value, but not change it
  public int getCheese() {
    return cheese;
  }

  //provide a setter like this if you want other classes to be able to change the value.
  public void setCheese(int cheese) {
    this.cheese = cheese;
  }

}

I have omitted the other fields for clarity. Note also that the getter and setter methods match the field name, except with get and set prepended, and the first letter of the field capitalized. The compiler does not require this, but it is accepted convention and good practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM