1

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);
}
}//
asked Oct 8, 2015 at 12:31
5
  • what's your confusion about classes and objects? Commented Oct 8, 2015 at 12:34
  • Title of your post is "...confusion about classes and objects," but the only question seems to be "how can I test it." In order to test it, you have to be able to describe conditions under which the program operates, and you have to be able to describe what the program is supposed to do under those conditions. The rest is easy: Run the program and see whether it does what it is supposed to do. Commented Oct 8, 2015 at 12:37
  • Just add the main metodo in the class and the program will start from there: docs.oracle.com/javase/tutorial/getStarted/application Commented Oct 8, 2015 at 12:37
  • @PetterFriberg its valid if you are using using java 7 Commented Oct 8, 2015 at 12:42
  • Thanks you are right still used to 1.4 ; ), will delete my comment Commented Oct 8, 2015 at 12:45

3 Answers 3

1

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 
 }
}
General Failure
2,6074 gold badges26 silver badges53 bronze badges
answered Jun 10, 2016 at 5:12
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Oct 8, 2015 at 12:43

2 Comments

thanks ,,yes all of that methods in the same class pizza but I mean I create the class but how I can create the objects of that class and how I can invoking the instance methods and variables
To make an instance of a class (a.k.a., an object), you use the Java new operator. For example, in your main() method, you can say: Pizza pizza = new Pizza(...); where ... are the actual values that you want to pass in to the constructor arguments.
0

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.

answered Oct 8, 2015 at 12:46

4 Comments

For switch see comment @ Bhargav Modi you can use String on switch with java 7 (I was also still in the world of java 1.4)
thanks for your help but what did u mean about enumerated types ? i didn't study it yet also the word ( this) in command this.cheese = cheese
An enumerated type can only have pre-defined values, for example, the type Size I defined can only have the value S,M or L. docs.oracle.com/javase/tutorial/java/javaOO/enum.html The keyword this simply means this object: docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
It is most definitely not "accepted convention" and "good practice". What you have described is the JavaBean specification, which is not required here. Java is verbose enough without adding unnecessary junk out of superstition.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.