۷۸۶
Subject :
JAVA OBJECT ORIENTED PROGRAMMING
Submitted Tto :
[Link]
10 Different Object Oriented Programs
CODED BY
Mohammad Neman Nikfarjam
ID 204
[Link] a program to find the average and sum of the N
numbers
import [Link];
//Imported package Needed for Input and Request Panel
public class average2{
public static void main (String [] args) {
int counter=0;
//Created and Initialized Variable for the Total Times of Entered Numbers
int score = 0;
//Created and Initialized Variable for the Added Amount of Scores Each Time
double sum=0;
//Created and Initialized Variable for the Added Up Total Numbers All Together
String input= [Link]("Enter Number!");
//Pop Up dialog Box for the input of the first number in String
score= [Link](input);
//Casting the String (input) In to Integer to make usable
while (score != 0) {
//Conditional While Loop
sum=sum+score;
//Adds Up the Total of the Variable Score into The variable Sum
input= [Link]("enter number or 0 to exit ");
//Loop Pop up dialog unless the number 0 Entered
score= [Link](input);
//Casting String Input into Integer Score
counter++;
//Counter Plus 1 for each input
}
String message= "the total is: "+sum+"\nand the average is: "+sum/counter;
[Link](null, message);
//the Show out System
}
}
[Link] a program to Create simple class to find the
Area and Perimeter of a Rectangle
import [Link];
public class AreaAndPerimeter {
public static void main (String [] args) {
String width1=[Link]("Please Enter The Width of the
Rectangle: ");
//graphical pop up box for the input of the Width of the rectangle
int width=[Link](width1);
//casting the input from String to integer
String Length1=[Link]("please Enter The length of the
Rectangle: ");
//graphical pop up box for the input of the length of the rectangle
int length=[Link](Length1);
//casting the input from string to integer
int Area= width*length;
//Main formula for finding the Area of a rectangle
int Perimeter=2*width+2*length;
//Main formula for finding the Perimeter of a Rectangle
[Link](null, "The Area Of the Rectangle is: "+Area+"\nThe
Perimeter of the Rectangle is: "+Perimeter
);
//last output including the Area and Perimeter of the rectangle
}
}
[Link] a Java Program to find the factorial of a given
number
import [Link];
public class factorial{
public static void main(String args[]){
int i,
//mentioned Variable
fact=1;
//mentioned and initiated Variable
String number1=[Link]("Enter a number to find its factorial");
//It is the input number to calculate factorial in string
int number=[Link](number1);
//casting the input from String to integer
for(i=1;i<=number;i++){
//first condition ,For loop
fact=fact*i;
//renews the value of the Fact
}
String message="Factorial of "+number+" is: "+fact;
[Link](null, message) ;
//final output show the factorial f the entered number
}
}
[Link] a Simple program which shows the Application
of constructors
class ConstructorMain {
private int x;
// constructor
private ConstructorMain(){
[Link]("Constructor Called");
x = 5;
}
public static void main(String[] args){
ConstructorMain obj = new ConstructorMain();
[Link]("Value of x = " + obj.x);
}
}
[Link] a program which shows the use of method
overloading
class Overloading {
private static void display(int a){
[Link]("Arguments: " + a);
}
private static void display(int a, int b){
[Link]("Arguments: " + a + " and " + b);
}
public static void main(String[] args) {
display(1);
display(1, 4);
}
}
[Link] a program Which show the use of the Static
Member
// Java program to demonstrate that a static member
// can be accessed before a class
class StaticMember
{
// static method
static void Member1()
{
[Link]("from Member1 \nSubmitted to [Link]");
}
public static void main(String[] args)
{
// calling m1 without creating
// any object of class Test
Member1();
}
}
[Link] a java Program which explains the concept of
single inheritance
class Teacher {
String designation = "Designation: Teacher ([Link])";
//first String showing the title of the job
String collegeName = "CollegeName: Hariwa University";
//second string showing the place of the job
void does(){
[Link]("Currently Active in Teaching ");
//shows the current condition of the employee
}
}
public class SingleIngeritance extends Teacher{
//inherits the class teacher
String mainSubject = "Main Subject: Programming";
//subject of the teacher
public static void main(String args[]){
SingleIngeritance obj = new SingleIngeritance();
//creating object from single inheritance
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]();
}
}
[Link] a program which shows the method overriding
class Human{
//Overridden method
public void eat()
{
[Link]("Human is eating");
}
}
class Boy extends Human{
//Overriding method
public void eat(){
[Link]("Boy is eating");
}
public static void main( String args[]) {
Boy obj = new Boy();
//This will call the child class version of eat()
[Link]();
}
}
[Link] a program which show s the set and get method
public class Set {
private String name; // private access
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String newName) {
[Link] = newName;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
///////
public class Get {
public static void main(String[] args) {
Set myObj = new Set();
[Link]("Ahmad"); // Set the value of the name variable to "ahmad"
[Link]([Link]());
}
}
[Link] a program which shows the dynamic
binding
public class DynamicBinding
{
public static class superclass
{
void print()
{
[Link]("print from superclass.");
}
}
public static class subclass extends superclass
{
void print()
{
[Link]("print from subclass.");
}
}
public static void main(String[] args)
{
superclass A = new superclass();
superclass B = new subclass();
[Link]();
[Link]();
}