Question

What is a class? Give an example, create a class What is a Method? Give an...

What is a class? Give an example, create a class

What is a Method? Give an example, create a method

Give an example of a Get Method

Give an example of a method with a return

What is a constructor? Create a constructor . Demonstrate that you understand class, and methods with a clear illustration.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Class:

A class is a blueprint or prototype from which objects are created

  • it is a template for creating objects.
  • it represents the set of properties or methods that are common to all objects of one type.

Syntax

        class <class_name>{  
             field;  
             method;  
          }  

Method:

A method is a block of code which only runs when it is called.

We can pass data, known as parameters, into a method.

return-type method-name parameter-list publicint max (int x, int y) { if (x> y) r modifier body of the method return x; else

Method with void return type means it doesn't return nothing to caller.

Syntax:

  static void myMethod() {
// code to be executed
}

Method with return type means it return something to caller with the help of return keyword.

Syntax:

   static int myMethod() {
// code to be executed

return 0;
}

Get Method:

  The get method returns the variable value also called as Accessor methods

Syntax:

  public class Person {
private String name; // private = restricted access

// Getter
public String getName() {
return name;
}

// Setter
public void setName(String newName) {
this.name = newName;
}
}

constructor:

A constructor is a block of codes similar to the method. which is is used to initialize the object.

  • It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory.
  • Every time an object is created using the new() keyword, at least one constructor is called.
  • It calls a default constructor if there is no constructor available in the class. In such case, Java compiler provides a default constructor by default.
  • There are two types of constructors in Java: no-arg constructor, and parameterized constructor.

Rules for creating Java constructor:

  1. Constructor name must be the same as its class name
  2. A Constructor must have no explicit return type
  3. A Java constructor cannot be abstract, static, final, and synchronized.

syntax:

  class Bike1{  

//creating a default constructor  

Bike1()

{

System.out.println("Bike is created");

}  

//main method  

public static void main(String args[]){  

//calling a default constructor  

Bike b=new Bike();  

}  

}  

Add a comment
Know the answer?
Add Answer to:
What is a class? Give an example, create a class What is a Method? Give an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (Strin...

    C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (String) edition ( int) published year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method// that return sting representation of Book object. 2-Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title...

  • I need this in java please Create an automobile class that will be used by a...

    I need this in java please Create an automobile class that will be used by a dealership as a vehicle inventory program. The following attributes should be present in your automobile class: private string make private string model private string color private int year private int mileage. Your program should have appropriate methods such as: default constructor parameterized constructor add a new vehicle method list vehicle information (return string array) remove a vehicle method update vehicle attributes method. All methods...

  • Create an Item class, which is the abstract super class of all Items.           Item class...

    Create an Item class, which is the abstract super class of all Items.           Item class includes an attribute, description of type String for every item. [for eg. Book]                                                             A constructor to initialize its data member of Item class.               Override toString() method to return details about the Item class. Create the ExtraCharge interface with the following details.                       Declare a constant RATE with value 0.25   Declare a method called calculateExtraCharge(), which returns a double value. Create the...

  • Your text has an example of LinkedList class. Convert the class so that the Link objects have a s...

    Your text has an example of LinkedList class. Convert the class so that the Link objects have a single long instance variable -- call it "data" -- and the Link instance variable.. Step 2 Then add the following capabilities to the class. A constructor that takes an array of longs as an argument. The contents of the array are use to create Link objects that become objects in the LinkedList. A "search" method that takes a long argument and returns...

  • Create a Mattress class should have fields for size (king, queen, twin), manufacturer, and price, and...

    Create a Mattress class should have fields for size (king, queen, twin), manufacturer, and price, and a constructor should set default values for each. Write set methods only for size and manufacturer. The set method for size adds $200 for king or $100 for queen. Add a toString() method that returns the data from all of the fields. A child class named AdjustableMattress extends Mattress and adds a field for adjustment type (air or mechanical), with air being the default....

  • Create a UML diagram to help design the class described in exercise 3 below. Do this...

    Create a UML diagram to help design the class described in exercise 3 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Baby class object; should they be private or public? Determine what class methods are required; should they be private or public? Write Java code for a Baby class. A Baby has a name of type String and an age of type integer. Supply two constructors:...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • Create a class Parent that has a method speaks() which shall display “Parents speaks” . Create...

    Create a class Parent that has a method speaks() which shall display “Parents speaks” . Create another class named Child that has a method sing() which shall display “ children also sing well”. Now, the Child class shall inherit the parent class. Write the Demo class to demonstrate the capabilities of the child class. (that is, create object of the Child class and call both the methods.)

  • Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super...

    Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super class. The class includes four private String instance variables: first name, last name, social security number, and state. Write a constructor and get methods for each instance variable. Also, add a toString method to print the details of the person. After that create a class Teacher which extends the class, Person. Add a private instance variable to store number of courses. Write a constructor...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT