Java: an object-oriented pizza

The tutor offers an illustration of a Java class.

Java is an object-oriented programming language. To tackle a problem, the programmer creates a class, which is an entity that holds (possibly) some data as well as (possibly) one or more functions, called methods.

For an example, here is a Java class called Pizza:

class Pizza{
String toppings=””;
public void describeMe(){
System.out.println(“I’m a pizza with these toppings: “+toppings);
}
Pizza(String[] toppingslist){//this is a constructor
for(int i=0;i<toppingslist.length;i++){
toppings+=toppingslist[i];
if(i<(toppingslist-2)){
toppings+=”, “;
}
else if(i==(toppingslist-2)){
toppings+=” and “;
}
}//end of for loop
toppings+=”.”
}
}//end of class Pizza

In coming posts I’ll provide more detail about the Pizza class shown here.

HTH:)

Source:

docs.oracle.com

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Leave a Reply