0% found this document useful (0 votes)
7 views12 pages

Object-Oriented Programming Examples

The document contains code for multiple Java programs that create classes with methods to calculate areas and perimeters of shapes like rectangles and triangles, print data types, accept student details, and demonstrate method overloading.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

Object-Oriented Programming Examples

The document contains code for multiple Java programs that create classes with methods to calculate areas and perimeters of shapes like rectangles and triangles, print data types, accept student details, and demonstrate method overloading.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

package rectangle;

class area{

double length;

double breadth;

void SETDIM(double length,double breadth){

[Link]=length;

[Link]=breadth;

public double GETAREA(){

double area=(length*breadth);

[Link]("area of rectangle"+(area));

return area;

public class RECTANGLE {

public static void main(String[] args) {

area ob=new area();

[Link](45.7, 90.7);

[Link]();

}
package student1;

class student{

String name;

int rollno;

student(String name,int rollno){

[Link]=name;

[Link]=rollno;

void show(){

[Link](name+"\n"+rollno);

public class Student1 {

public static void main(String[] args) {

student so=new student("john",2);

[Link]();

}
package [Link];

class printnumber{

int a;

float b;

double c;

boolean d;

void integer(int a)

[Link](a);

void flo(float b){

[Link](b);

void dou(double c){

[Link](c);

public class DataTypes {

public static void main(String[] args) {

printnumber sc=new printnumber();

[Link](5);

[Link](0.05);

[Link](67.9);

}
}
package [Link];

class student{

int n;

char c;

void name(int n,char c){

this.n=n;

this.c=c;

[Link]("n="+n+"c="+c);

void name2(char c,int n){

this.c=c;

this.n=n;

[Link]("c="+c+"n="+n);

public class TwoMethods {

public static void main(String[] args) {

student sc=new student();

[Link](10,'s');

sc.name2('s',10);

}
}
package [Link].r;

class square{

double s;

double l,w;

void rec(double s){

[Link](s*s);

void rec(double l,double w){

[Link](l*w);

public class AreaOfSAndR {

public static void main(String[] args) {

square ob=new square();

[Link](31.1);

[Link](62.1,78.6);

}
package twostudents;

class student{

String name;

int rollno;

int phoneno;

String address;

void s1(String name,int rollno,int phoneno,String address){

[Link]=name;

[Link]=rollno;

[Link]=phoneno;

[Link]=address;

void show(){

[Link](name+"\n"+rollno+"\n"+phoneno+"\n"+address);

public class Twostudents {

public static void main(String[] args) {

student so=new student();

student s22=new student();

so.s1("john",02,0346,"house 148 A");

s22.s1("sam",02,0345,"house 150A");

[Link]();

[Link]();

}
package [Link];

import java .[Link];

class triangle{

double s1=3;

double s2=4;

double s3=5;

triangle(){

double semiperimeter=(s1+s2+s3)/2.0;

double area=[Link](semiperimeter*(semiperimeter-s1)*(semiperimeter-s2)*(semiperimeter-
s3));

[Link]("area of triangle is"+area);

double perimeter=s1+s2+s3;

[Link]("perimeter of triangle is"+perimeter);

public class [Link]{

public static void main(String[]args){

triangle t=new triangle();

}
package main;

class triangle{

double s1;

double s2;

double s3;

triangle(double side1,double side2;double side3){

s1=side1;

s2=side2;

s3=side3;

double semiperimeter=(s1+s2+s3)/2.0;

double area=[Link](semiperimeter*(semiperimeter-s1)*(semiperimeter-s2)*(semiperimeter-
s3));

[Link]("area of triangle is"+area);

double perimeter=s1+s2+s3;

[Link]("perimeter of triangle is"+perimeter);

public class Main {

public static void main(String[] args) {

triangle t=new triangle(3.0,4.0,5.3);

Common questions

Powered by AI

In "area.and.perimeter.of.triangle", the "triangle" constructor does not take parameters and uses fixed side values, leading to a single instantiation mode with fixed dimensions. In contrast, the "main" package's constructor accepts parameters (side1, side2, side3), allowing varied instantiation with different side lengths, offering greater flexibility .

In the "student1" package, the constructor in "student" initializes the class fields name and rollno directly when an object is instantiated. Whereas in the "twostudents" package, the constructor pattern is replaced by a method "s1" to initialize the fields, allowing the use of default constructors. This structural difference reflects varying initialization strategies .

The "double" data type in the "triangle" class is crucial for accurately calculating the area and perimeter, which often involve non-integer results such as square roots. Its precision helps ensure that calculated areas, especially with semiperimeter-based formulas, are correct and that operations on potentially large or very small quantities retain accuracy .

The document does not demonstrate explicit error handling mechanisms such as try-catch blocks or exception handling typically found in robust Java applications. All classes assume correct input types and values, indicating that managing potential runtime errors is not prioritized in these implementations .

The "area" class illustrates encapsulation by keeping its fields (length, breadth) private and providing public methods SETDIM() and GETAREA() for setting and retrieving these values. This restricts direct access to the fields and ensures that length and breadth can only be modified through the method, protecting the integrity of the data .

The "show" method in "student1" directly displays the student's name and roll number, while in "twostudents," "show" displays additional information (name, roll number, phoneno, and address). This illustrates a more detailed approach in "twostudents," expanding the output to multiple data attributes and enriching the context available for each student entry .

Method overloading in the "square" class allows the same method name 'rec' to be used for calculating the area of both a square and a rectangle by differentiating the methods on the basis of the number and types of parameters . This approach improves code readability and reusability as developers can use the same method name for different operations based on context.

Choosing between a method or constructor for assigning values involves considering initial value state, flexibility, readability, and maintenance. Constructors ensure immediate initialization, enforcing field consistency and object readiness, while methods provide flexibility, allowing deferred or condition-based initialization and reusability. Methods can make classes more adaptable to change without altering constructor definitions .

Inheritance is an OOP principle where a class can inherit attributes and methods from another class. The document does not exhibit classical inheritance as all classes are independently defined with no use of the "extends" keyword to inherit from a superclass. This absence signifies direct implementation without leveraging hierarchical class relationships .

Using "System.out.println" consistently allows for straightforward output to the console, enhancing readability by simplifying how results and status messages are communicated. However, it limits usability when differing output media are needed or when more complex output formatting is required. The verbosity can clutter logic in more detailed or large-scale programs .

You might also like