Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

Object Oriented Programming in Java

The document discusses various mathematical operators, functions, and concepts related to object oriented programming in Java including arrays, methods, classes, and objects. Mathematical operators like addition, subtraction, multiplication, and modulus are covered. Functions such as random, round, touppercase, and tolowercase are also discussed.

Uploaded by

abu.boitech
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Object Oriented Programming in Java

The document discusses various mathematical operators, functions, and concepts related to object oriented programming in Java including arrays, methods, classes, and objects. Mathematical operators like addition, subtraction, multiplication, and modulus are covered. Functions such as random, round, touppercase, and tolowercase are also discussed.

Uploaded by

abu.boitech
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

OBJECT ORIENTED PROGRAMMING IN JAVA.

MATHEMATICAL OPERATORS.
These operators are used to handle arithmetic expressions in a program. They are used as
discussed below.
Addition operator (+).
It is used to add two or more operands together. The operands could be the real values like 4 + 6,
or represented by variables like x + y. To output the results of the program, the output statement
could be written as, System.out.println (“x + y =” + (x + y));
Subtraction operator (+).
It is used to subtract one operand from another, for example 6 – 4 or y – x. The output statement
of subtraction is written as, System.out.println (“y – x =” + (y – x));
Multiplication operator (*)
It is used to multiply two or more operands that are assigned in the program or are supplied by
the user. To output the results of the program, the output statement could be written as,
System.out.println (“x * y = ” + (x * y));
Division operator (/).
The division operator is used to divide a numerator operand by the denominator operand as 6/4
or x/y. To output the results of the program, the output statement could be written as,
System.out.println (“x / y = ” + (x / y));
Modulus operator (%).
It is an operator that is used to return the remainder of the division of two operands, for example
5%2 =1, because 5/2 = 2 remainder 1. To output the results of the program, the output statement
could be written as, System.out.println (“x % y =” + (x % y));
MATHEMATICAL FUNCTIONS.
Math. Pow()function.
It is a function that is used to return the power of a number, i.e. a base operand is raised with a
exponent operand. The function can be declared in the program as followed;
type pow( type x, type y); where type is the data type, x is the base number and y is the exponent.
For example suppose x = 2 and y = 3, thus int pow(int x, int y) =23 =8. The ouput of the above
program can be written as; System.out.println("POW: " + i_a + " power " + i_b + ");
Math. Random ( ) function.
Random function is a function that generates any random numbers between 0.0 and 1.0. For
example a program to display any numbers at random could be written as;
System.out.println("Some random numbers are: " + Math. Random ( )); this could output
anything like 0.123456789.
Math. Round ( ) function.
Round function is a function that Rounds off a functional parameter value by first adding 0.5 to it
and then returning the largest value that is less than or equal to that new value. Round function
can take different parameters as follows:
round( float x) - returns the closest float number to the argument x.
round( double x) - returns the closest long float number to the argument x.
CHARACTER AND STRING FUNCTIONS.
String Functions are functions that manipulate strings to behave in different forms. They include:
touppercase( ) function.
Toppercase function is a function that is used to convert all strings or characters to uppercase.
For example, suppose a string store = “check this case”, then
System.out.println(store.toUpperCase()); - will display “CHECK THIS CASE”
tolowercase( ) function.
tolowercase function is a function that is used to convert all strings or characters to lower case.
For example, suppose a string store = “CHECK THIS CASE”, then
System.out.println(store. toUpperCase()); - will display “check this case”
SUB STRINGS.
indexOf()function.
indexOf () function is use to return index of given sub string character values starting from 0,
upon which if it fails will return -1. It comprises of four types as discussed below.
int indexOf(int character) - returns position index for a given character value.
int indexOf(int character, int StartIndex) - returns index position of a given character value and
the index starting from.
int indexOf(String substr) – returns position index of given substring.
int indexOf(String substr, int StartIndex) - returns position index of a given substring and the
index starting from.
ASCII Characters and Strings.
Java supports all most types of characters and strings including that of American Standard Code
for Information Interchange. There are up to 128 characters supported by ASCII java e.g.? @ $
% 0 1 3 a c etc. To display all of these characters the program in the following format.
for (int n=1;i<=127;n++)
System.out.println("Number: " + n + " Character = " +(char)n);
You can achieve file processing in Java by use of various classes. The main class used to handle
files is the File. The File class is among the java.io libraries. You can use it by importing it to the
file. i.e.
import java.io.File;
For you to use a file you need to declare it first. A file is declared by use of one of its
constructors. The constructor takes the argument as a string, the file name or its path. The file
extension must be included. For example:
File fileStore = new File("filename.ext");
FILE OBJECTS.
File object is a tool that is created so as to help in manipulation of a file. The manipulation will
include opening of the file for reading from and writing to. The file is opened for reading and
writing by use of a scanner. Therefore, in each instance a scanner is needed it must be created.
You can create the scanner as follows:
Scanner <name> = new Scanner(new File("<file name>"));
Scanner myscanner = new Scanner(new File("myfile.txt"));
File Methods.
Exists( ) method is used to find if the file exists an if it can be read. You can write a program to
determine the existence of the file as follows.
while (file.exists()) { System.out.println("File successfully found! ");}
else { System.out.println("File not found! Try again: ");}
Next Methods.
Methods that uses next determines the nature of the next input parameter from the scanner. They
include:
next( ) determines whether more parameters are remaining.
nextInt( ) determines whether the next parameter to be read is an integer.
nextdouble( ) determines whether the next parameter to be read is a double.
nextline( ) determines whether there are more lines remaining to be read.
Close( ) method.
Close( ) method is used to close an open file after it has outputted its contents e.g. output.close();

ARRAYS.
Arrays are storage locations that store collection of data with similar data types. Array
declaration takes the format: type ArrayName[subscript]; eg int StoreInt[6];
Array sizes.
Arrays are of varying sizes. The determining factor of an array size are its data types and its
dimension. There are different dimensions of an array, as discussed below.
Single Dimensional Array.
Single dimensional array is an arra
int[][] board = new int[ROWS][COLS];
Looping In an Array.
To generate a set of data automatically in a pro y with only one subscript eg. int StoreInt[6];
Two-dimensional Array.
Two-dimensional array is an array with two subscripts. It is used to handle data that is in two
dimensions such as that of rows and columns. For example, an array of a table with 5 rows and 4
columns could be declared as follows:
int ROWS = 5, int COLS = 4;
gram through an array, looping statements are used. For example:
for ( i = 0 ; i < 10 ; i++ ){ // Print column j in row i
for ( j = 0 ; j < a[i]; j++ )
{ System.out.print( a[i][j] + " " ); }
METHODS.
A method is a collection of statements that are grouped together to perform a given operation.
Method Header.
Method header is just a declaration of a method to be used in the program. Method header just
introduces the type, name and the scope where the method will be used i.e. whether public or
private. For example: public int Calc(int x, int y);
Method Calling.
After the method declaration, any statement or another function can call it. When a method is
called, arguments are supplied for it to act on them. After the method has acted on the arguments,
it returns them to the calling program. Illustration:
public int Calc(int x, int y) { int a = 4; int b = 6; int c = Calc(int x, int y)
System.out.println("Result = " + c); }
Passing and Returning an Array to and from a Method.
Arrays can also be passed as arguments to a method, after which a a method will return the
results to the calling program. Illustration:
public int Average(int[] array ) { int total =0, int Avg = 0; for(int n=0; n < 10; n++) {total =
total + array[n];
Avg = total / 10; } return Avg;
SCOPE OF VARIABLES.
Local and Field variables.
Local variables are variables declared and used within a given method only. For example:
Int Calculate(int x, int y ) { int LocalVar1 , int LocalVar2; }
Field variables.
Field variables are variables declared and used all over in the general program. For example:
Public void main ( ) { int Field Var1 , int FieldVar2; }
CLASSES.
A class is a blueprint through which an object is derived from it. An object therefore is an
instance of a class.
Class Access Specifiers.
Access specifiers are class regulators that monitors the visibility of class members in the
program. They include public, private and protected.
Public Access Specifier declares class member data to be open and can be accessed by other
classes within the same program project.
Private Access specifier limit access of class member data only to the class that it was declared
in.
Protected Access Specifier limits and hide some of the class member data that carry sensitive
information.
Illustration:
Class Account{ public GetuserName( ); public GetPassword( ); private UserName;
protected Password; };
Accessor Method - Get( ).
Accessor Method is a function that adds functionality to a method by enabling it to obtain a
value. For example: getArea( );
Mutator Method – Set( ).
Mutator method adds functionality to a method by enabling it to change an existing parameter to
a new parameter. For example int setArea( );
INSTANTIATING OBJECTS
Object instantiation is creation of a new object from a class. The new key word is used to
instantiate an object. For example if there was a a class named shapes, then a real shape object
like rectangle will be generated as: Shape = new Rectangle( );
CONSTRUCTORS AND DESTRUCTORS.
Constructors are methods that brings a class into existence while destructors clears the classs
after it has accomplished its purpose in the program.
Constructors that are generated by automatically by the compiler are known as default
constructors. They use the same name as that of the class.
Copy constructors are constructors that are used to generate a copy of objects and that needs to
be reproduced more than once.
GUI PROGRAMMING.
GUI Programming is developing applications that in the Graphical User Interface platform. GUI
Programming facilitate such features as menus, files, icons and buttons.
Stage Class.
Stage class is a class library function with properties that are used to set the appearance of an
application. The stage class styles include decorated, undecorated, transparent and utility.
Pane Class.
Pane class exposes the derived class as public so that the users of the subclass are free to use
them.
Scene.
The Scene interface is a set of functions used to extract Java 3D scene graph information from a
file. It gives loaders of various file formats a common public interface.
STEPS IN CREATING A GUI APPLICATION.
1. Inserting a form.
2. Placing and arranging objects in the form.
3. Assigning properties to the form.
4. Writing codes to drive the objects.
5. Making forms to be functional.
6. Previewing forms.
APPLICATION.LAUNCH( ).
Application.launch( )method is used to start an application that has been build and is ready to
run. It takes the form: public static void main(String[] args) { launch(args);}
Start( ) Method.
Start( ) method is used to prepare the a process to be executed by a run( ) method. It takes the
format: public void start()
Handle()Method.
Handle method is used to get a reference to another Method.
How to Create A Line.

public class DrawLine2D extends JApplet { Graphics2D line = (Graphics2D);


line.draw(new Line2D.Double(x, y, 200, 200));
How to Create A Circle.
public void DrawCircle(Graphics2D g, int x, int y, int r) { x = x-(r/2); y = y-(r/2); }
How to Create A Rectangle.
void drawPolygon(int x[], int y[], int numOfPoints)

You might also like