Recitation2

Recitation2 - CS180 Recitation 2 Java classes Classes are...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
CS180 Recitation 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Java classes Classes are the building blocks of Java applications. A Java application can have any number of classes. General form of a Java class class CLASSNAME { returntype METHODNAME1(ParameterType PARAMETERNAME1, ParameterType PARAMETERNAME2, . .) { STATEMENTS; // body of the method } returntype METHODNAME2(ParameterType PARAMETERNAME1, ParameterType PARAMETERNAME2, . .) { STATEMENTS; // body of the method } }
Background image of page 2
Methods A class can have any number of methods. main (special method) provides the entry point for a java application where execution begins. Only one main method allowed in a java application. Java does not allow methods outside class definitions. Methods are comprised of 4 parts Name of the method Return type of the method List of parameters Body of the method
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example class Example { public static void main(String a[]) { System.out.println(add(3,4)); } private static int add(int num1 , int num2) { int sum; sum = num1 + num2; return sum; } }
Background image of page 4
Creating objects In java objects are created using the new operator. new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. "instantiating a class" and "creating an object” both mean the same. Example: Student student = new Student(); The above statement creates a new object of type Student and assigns it to the variable student.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example // calculates sum of 2 complex numbers of form a+ib class Complex { int real; // real part of complex number int imaginary; // imaginary part of complex number }
Background image of page 6
Example contd. . public class Example{
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/26/2012 for the course STAT 350 taught by Professor Staff during the Spring '08 term at Purdue.

Page1 / 18

Recitation2 - CS180 Recitation 2 Java classes Classes are...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online