Defining a Java class Assignment Help

Assignment Help: >> Classes in Java >> Defining a Java class

Defining a Java class:

The general structure of a Java class is as follows:

class ClassName
{
Data declarations
Method definitions

}

The Java keyword class is used with an appropriate identifier as the name of the class. Class names conventionally start with an upper-case letter. The body of the class is delimited by curly brackets and contains both data declarations and details of the methods for the class. These items may be arranged in any order, but we will follow the convention of listing data declarations first, followed by method definitions, as shown above.

For example, the first part of the Java class definition for a robot is shown below:

public class Robot
{
private int x; // x-position(W-E) of robot
private int y; // y-position(N-S) of robot
...

}

This shows that the data for objects of the Robot class consists of two instance variables, x and y, which define where the robot is on the grid; these variables will hold integer values. These instance variables are declared as private, to ensure that this data cannot be directly accessed by objects of other classes.

The rest of the definition of the Robot class is made up of definitions of the methods that can be invoked on Robot objects. The definition of the moveNorth method is shown below:

public void moveNorth ()

{

y=y+1;

}

The first line of the definition, the method header, begins with two Java keywords, public and void. Methods are normally declared public, which means that they can be invoked by objects of other classes. The keyword void indicates that the method does not return a value. These keywords are followed by the name of the method and a list of its arguments; in this case there are no arguments, so the brackets are empty. 

The method body is enclosed in curly brackets - this defines the code to be executed when the method is invoked. In this case, it consists of a single statement that increments the value of the instance variable y. This has the effect of moving the current position of the Robot object one grid square to the north. In the discussion that follows, we shall ignore the possibility that a robot might attempt to travel beyond the limits of the grid.

The definition of the moveSouth method is similar:

public void moveSouth ()

{

y=y - 1;

}

This decrements the instance variable y by one, hence moving the robot one square to the south.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Defining a Java class  homework and assignments? Live Defining a Java class  experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Defining a Java class  homework help, java assignment help and Defining a Java class  projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd