What Is A Class

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 9

1.

Define and give example for class, object, constrictor and Method Class
In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created. Example
class Bicycle { int cadence = 0; int speed = 0; int gear = 1; void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; } void speedUp(int increment) { speed = speed + increment; } void applyBrakes(int decrement) { speed = speed - decrement; } void printStates() { System.out.println("cadence:" + cadence + " speed:" + speed + " gear:" + gear); }

Object
Objects are key to understanding object-oriented technology. Look around right now and you'll find many examples of real-world objects: your dog, your desk, your television set, your bicycle. Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing

gear, changing pedal cadence, applying brakes). Identifying the state and behavior for realworld objects is a great way to begin thinking in terms of object-oriented programming. Take a minute right now to observe the real-world objects that are in your immediate area. For each object that you see, ask yourself two questions: "What possible states can this object be in?" and "What possible behavior can this object perform?". Make sure to write down your observations. As you do, you'll notice that real-world objects vary in complexity; your desktop lamp may have only two possible states (on and off) and two possible behaviors (turn on, turn off), but your desktop radio might have additional states (on, off, current volume, current station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune). You may also notice that some objects, in turn, will also contain other objects. These real-world observations all translate into the world of object-oriented programming. Example
public class CreateObjectDemo { public static void main(String[] args) { // Declare and create a point object // and two rectangle objects. Point originOne = new Point(23, 94); Rectangle rectOne = new Rectangle(originOne, 100, 200); Rectangle rectTwo = new Rectangle(50, 100); // display rectOne's width, // height, and area System.out.println("Width of rectOne: " + rectOne.width); System.out.println("Height of rectOne: " + rectOne.height); System.out.println("Area of rectOne: " + rectOne.getArea()); // set rectTwo's position rectTwo.origin = originOne; // display rectTwo's position System.out.println("X Position of rectTwo: " + rectTwo.origin.x); System.out.println("Y Position of rectTwo: " + rectTwo.origin.y); // move rectTwo and display // its new position rectTwo.move(40, 72); System.out.println("X Position of rectTwo: " + rectTwo.origin.x); System.out.println("Y Position of rectTwo: " + rectTwo.origin.y);

Constructors
A constructor is a special method that is used to initialize a newly created object and is called just after the memory is allocated for the object It can be used to initialize the objects ,to required ,or default values at the time of object creation It is not mandatory for the coder to write a constructor for the class If no user defined constructor is provided for a class, compiler initializes member variables to its default values.

numeric data types are set to 0 char data types are set to null character(\0) reference variables are set to null

In order to create a Constructor observe the following rules 1) It has the same name as the class 2) It should not return a value not even void

Example
public class ConstructorOverloading{ public static void main(String args[]){ Rectangle rectangle1=new Rectangle(2,4); int areaInFirstConstructor=rectangle1.first(); System.out.println(" The area of a rectangle in first constructor is : " + areaInFirstConstructor); Rectangle rectangle2=new Rectangle(5); int areaInSecondConstructor=rectangle2.second(); System.out.println(" The area of a rectangle in first constructor is : " + areaInSecondConstructor); Rectangle rectangle3=new Rectangle(2.0f); float areaInThirdConstructor=rectangle3.third(); System.out.println(" The area of a rectangle in first constructor is : " + areaInThirdConstructor); Rectangle rectangle4=new Rectangle(3.0f,2.0f); float areaInFourthConstructor=rectangle4.fourth(); System.out.println(" The area of a rectangle in first

constructor is : } }

" + areaInFourthConstructor);

class Rectangle{ int l, b; float p, q; public Rectangle(int x, int y){ l = x; b = y; } public int first(){ return(l * b); } public Rectangle(int x){ l = x; b = x; } public int second(){ return(l * b); } public Rectangle(float x){ p = x; q = x; } public float third(){ return(p * q); } public Rectangle(float x, float y){ p = x; q = y; } public float fourth(){ return(p * q); } }

Methods
The only required elements of a method declaration are the method's return type, name, a pair of parentheses, (), and a body between braces, {}. More generally, method declarations have six components, in order: 1. Modifierssuch as public, private, and others you will learn about later. 2. The return typethe data type of the value returned by the method, or void if the method does not return a value. 3. The method namethe rules for field names apply to method names as well, but the convention is a little different. 4. The parameter list in parenthesisa comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses. 5. An exception listto be discussed later. 4

6. The method body, enclosed between bracesthe method's code, including the declaration of local variables, goes here. Modifiers, return types, and parameters will be discussed later in this lesson. Exceptions are discussed in a later lesson. Definition: Two of the components of a method declaration comprise the method signaturethe method's name and the parameter types. Example Here is an example of a typical method declaration:
public double calculateAnswer(double wingSpan, int numberOfEngines, double length, double grossTons) { //do the calculation here }

2. Write a java program that gives the following output


import java.io.*; class triangle{ public static void main(String[] args) { try{ BufferedReader object = new BufferedReader (new InputStreamReader(System.in)); System.out.println("enter the number"); int a= Integer.parseInt(object.readLine()); for (int i=1; i<a;i++ ){ for (int j=1; j<=i;j++ ){ System.out.print("*"); } System.out.println(""); } } catch(Exception e){} } } Outupte * ** *** ****

***** ****** -----------------------------------------------------------------------------------------------------------import java.io.*; class triangle{ public static void main(String[] args) { try{ BufferedReader object = new BufferedReader (new InputStreamReader(System.in)); System.out.println("enter the number"); int a= Integer.parseInt(object.readLine()); for (int i=1; i<a;i-- ){ for (int j=1; j<=i;j-- ){ System.out.print("*"); } System.out.println(""); } } catch(Exception e){} } } Outupte ****** ***** **** *** ** *

3. write a java program that takes two integer values and gives an output for summation, multiplication, subtraction and division of the two inputs.
import java.util.Scanner; public class Sample3 { static int x , sum , y , sub , mul , div; public static void main ( String args[] ) { Scanner scan = new Scanner(System.in); System.out.print("Enter first number>:"); x = scan.nextInt(); System.out.print("Enter second number>:"); y = scan.nextInt(); sum(); subtract(); multiply(); divide(); } public static int sum ( ) { sum = x + y; System.out.println("The sum is >:" + sum); return sum; } public static int subtract ( ) { sub = x - y; System.out.println("The subtract is >:" + sub); return sub; } public static int multiply ( ) { mul = x * y; System.out.println("The multiple is >:" + mul); return mul; } public static int divide ( ) 7

{ div = x / y; System.out.println("The divide is >:" + div); return div; } }

4. Write a program to calculate factorial of any given number import java.io.*; class Factorial{ public static void main(String[] args) { try{ BufferedReader object = new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter the number"); int a= Integer.parseInt(object.readLine()); int fact= 1; System.out.println("Factorial of " +a+ ":"); for (int i= 1; i<=a; i++){ fact=fact*i; } System.out.println(fact); } catch (Exception e){} } }

5. Write a program that calculate the net salary of an employee


My code is supposed to take a figure for the Gross Salary from the User and computes the tax which is 30% of gross and then print the Net Salary which is Gross-tax. Ihave tried this one and it shows 6 errors which have challenged me.
import java.util.Scanner; public class salary { public static void main(String[] args) { System.out.println("Hello Boy!"); // just a greeting Scanner money=new Scanner(System.in);

salary

System.out.println("Enter Gross:"); double gross = money.nextDouble(); // allows input of the Gross

double paye = paye(gross); double net = net(gross,paye); System.out.println("The Net salary is: "+net);

} public static double paye(double gross){ return 0.3*gross; // calculates the salary } public static double net(double gross, double paye){ return gross-paye; // calculates the Net salary } }

You might also like