Docsity

Maths Functions and Array of Objects in Java programming, Study notes of Object Oriented Programming

Partial preview of the text

Download Maths Functions and Array of Objects in Java programming and more Study notes Object Oriented Programming in PDF only on Docsity!

Array of objects

By-Ms.Aquilla Afonso

Asst.Professor

St.Xavier’s College-Goa

What Is An Array Of Objects?

The array elements store the location of the

reference variables of the object.

An object represents a single record in

memory, and thus for multiple records, an

array of objects must be created. It must be

noted, that the arrays can hold only

references to the objects, and not the

objects themselves.

Syntax :

Class obj[]= new Class[array_length]

Variation of arrays of objects

Rectangle.java Main.java

public class Rectangle { double length; double width; public void setdata(double l,double w) { length=l; width=w; } public void showdata() { System.out.println("the lenth is"+length+"width is"+width); double area=length*width; System.out.println("the area is"+area); } } import java.util.Scanner;public class Main{ public static void main(String[] args) { Rectangle rect[]= new Rectangle[5]; Scanner a= new Scanner(System.in); for(int k=0;k<5;k++) { rect[k]=new Rectangle(); } for(int i=0;i<5;i++) { System.out.println("enter length of rect"+i+1+":"); double length= a.nextDouble(); System.out.println("enter breadth of rect"+i+1+":"); double width= a.nextDouble(); rect[i].setdata(length,width); } for(int j=0;j<5;j++) { rect[j].showdata(); } } }

Variation of arrays of objects

Rectangle.java Main.java

public class Rectangle { double length; double width; Rectangle(double l,double w) { length=l; width=w; } public void showdata() { System.out.println("the length is"+length+"width is"+width); double area=length*width; System.out.println("the area is"+area); } } import java.util.Scanner; public class Main { public static void main(String[] args) { Rectangle rect[]= new Rectangle[5]; Scanner a= new Scanner(System.in); for(int i=0;i<5;i++) { System.out.println("enter length of rect"+i+1+":"); double length= a.nextDouble(); System.out.println("enter breadth of rect"+i+1+":"); double width= a.nextDouble(); rect[i]=new Rectangle(length,width); } for(int j=0;j<5;j++) { rect[j].showdata();

Basic Math Functions

Math.ceil()

 The Math.ceil() func

tion rounds a

floating point value

up to the nearest

integer value. The

rounded value is

returned as

a double. Here is

a Math.ceil() Java

example:

double x = 83.56; System.out.println(Math.c eil(x)); Output

Basic Math Functions

 Math.floor()

The Math.floor() function
rounds a floating point
value down to the nearest
integer value. The rounded
value is returned as
a double. Here is
a Math.floor() Java example:

double x = 94.69; System.out.println(Math.floo r(x)); Ouput

Basic Math

Functions

 Math.round() The Math.round() method rounds a float or double to the nearest integer using normal math round rules (either up or down). Here is a Java Math.round() example: Add 10 more math functions to this list double x = 79.52; System.out.println(Math.round(x)); OUTPUT: 80

THANK YOU !!!