0% found this document useful (0 votes)
80 views

Computer Project Programs

The document contains code for multiple Java programs that demonstrate different concepts: 1) A program that reads a matrix from user input and displays the entered elements. 2) A program that creates Rectangle objects from user input and calculates their areas. 3) Additional programs demonstrate bubble sort, binary search, pattern printing, inheritance, object creation, and more.

Uploaded by

Ruhi Bhatt
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Computer Project Programs

The document contains code for multiple Java programs that demonstrate different concepts: 1) A program that reads a matrix from user input and displays the entered elements. 2) A program that creates Rectangle objects from user input and calculates their areas. 3) Additional programs demonstrate bubble sort, binary search, pattern printing, inheritance, object creation, and more.

Uploaded by

Ruhi Bhatt
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 26

// Java program to read a matrix from user

import java.util.Scanner;
Public class MatrixFromUser
{
// Function to read matrix
Public static void readMatrixByUser()
{
int m, n, I, j;
Scanner in = null;
Try
{
In = new Scanner(System.in);
System.out.println(“Enter the number “
+ “of rows of the matrix”);
N= in.nextInt();
System.out.println(“Enter the number “
+ “of columns of the matrix”);
N = in.nextInt();
// Declare the matrix
Int first[][] = new int[m][n];
// Read the matrix values System.out.println(“Enter the elements of the matrix”);
For (I = 0; I < m; i++)
For (j = 0; j < n; j++)
First[i][j] = in.nextInt();
// Display the elements of the matrix
System.out.println(“Elements of the matrix are”);
For (I = 0; I < m; i++) {
For (j = 0; j < n; j++)
System.out.print(first[i][j] + “ “);
System.out.println();
}
}
Catch (Exception e) {
}
Finally {
In.close();
}
}
// Driver code
Public static void main(String[] args)
{
readMatrixByUser();
}
}

Output:
Enter the number of rows of the matrix 2
Enter the number of columns of the matrix 2
Enter the elements of the matrix
1
2
3
4
Elements of the matrix are
12
34

Import java.util.*;
Public class ArrayOfObjects
{
Public static void main(String[] args)
{
// Create an array to hold rectangle’s data.
Rectangle[] rectangles = new Rectangle[5];
Double length; //to hold length of rectangle
Double width; //to hold width of rectangle
// Create a Scanner object for keyboard input.
Scanner console = new Scanner(System.in);
Rectangle[] rectangles = new Rectangle[5];
For (int I = 0; I < 5; i++)
{
System.out.print(“Enter the length of Rectangle “ + (I + 1)
+ “: “);
Length = console.nextDouble();
System.out.print(“Enter the width of Rectangle “ + (I + 1)
+ “: “);
Width = console.nextDouble();
Rectangles[i] = new Rectangle(length, width);
System.out.println();
}
For (int I = 0; I < 5; i++)
{
System.out.println(“Area of Rectangle “ + (I + 1) + “: “
+ rectangles[i].getArea());
}
}
}

Output :
Enter the length of Rectangle 1: 12
Enter the width of Rectangle 1: 3

Enter the length of Rectangle 2: 5


Enter the width of Rectangle 2: 4

Enter the length of Rectangle 3: 1.5


Enter the width of Rectangle 3: 2.2

Enter the length of Rectangle 4: 6


Enter the width of Rectangle 4: 7

Enter the length of Rectangle 5: 8


Enter the width of Rectangle 5: 2

Area of Rectangle 1: 36.0


Area of Rectangle 2: 20.0
Area of Rectangle 3: 3.30
Area of Rectangle 4: 42.0
Area of Rectangle 5: 16.0
import java.util.Scanner;

Class BubbleSort
{
Public static void main(String []args) {
Int n, c, d, swap;
Scanner in = new Scanner(System.in);

System.out.println(“Input number of integers to sort”);


N = in.nextInt();
Int array[] = new int[n];

System.out.println(“Enter “ + n + “ integers”);

For (c = 0; c < n; c++)


Array[c] = in.nextInt();

For (c = 0; c < ( n – 1 ); c++) {


For (d = 0; d < n – c – 1; d++) {
If (array[d] > array[d+1]) /* For descending order use < */
{
Swap = array[d];
Array[d] = array[d+1];
Array[d+1] = swap;
}
}
}

System.out.println(“Sorted list of numbers”);

For (c = 0; c < n; c++)


System.out.println(array[c]);
}
}

Output:

Input number of integers to sort


5
Enter 5 integers
5
4
3
2
1
Sorted list of numbers
1
2
3
4
import java.util.Scanner;

Class BinarySearch
{
Public static void main(String args[])
{
Int c, first, last, middle, n, search, array[];

Scanner in = new Scanner(System.in);


System.out.println(“Enter number of elements”);
N = in.nextInt();
Array = new int[n];

System.out.println(“Enter “ + n + “ integers”);

For (c = 0; c < n; c++)


Array[c] = in.nextInt();

System.out.println(“Enter value to find”);


Search = in.nextInt();

First = 0;
Last = n – 1;
Middle = (first + last)/2;

While( first <= last )


{
If ( array[middle] < search )
First = middle + 1;
Else if ( array[middle] == search )
{
System.out.println(search + “ found at location “ + (middle + 1) + “.”);
Break;
}
Else
Last = middle – 1;

Middle = (first + last)/2;


}
If ( first > last )
System.out.println(search + “ is not present in the list.\n”);
}
}

Output:
Enter number of elements
5
Enter 5 integers
2
5
6
8
9
Enter value to find
5

Import java.util.Scanner;
Public class Edureka
{
Public static void main(String args[])
{
Int n, I, j, space = 1;
System.out.print(“Enter the number of rows: “);
Scanner s = new Scanner(System.in);
N = s.nextInt();
Space = n – 1;
For (j = 1; j<= n; j++)
{
For (I = 1; i<= space; i++)
{
System.out.print(“ “);
}
Space--;
For (I = 1; I <= 2 * j – 1; i++)
{
System.out.print(“*”);
}
System.out.println(“”);
}
Space = 1;
For (j = 1; j<= n – 1; j++)
{
For (I = 1; i<= space; i++)
{
System.out.print(“ “);
}
Space++;
For (I = 1; i<= 2 * (n – j) – 1; i++)
{
System.out.print(“+”);
}
System.out.println(“”);
}
}
}

Output
+
+++
+++++
+++++++
+++++++++
+++++++
+++++
+++
+
Import java.util.Scanner;
Public class JigSawAcademy
{
public static void main(String[] args)
{
For (int I = 5; I >= 0; i–)
{
Int alphabet = 65;
For (int j = 0; j <= I; j++)
{
System.out.print((char) (alphabet + j) + ” “);
}
System.out.println();
}
For (int I = 0; i<= 5; i++)
{
Int alphabet = 65;
For (int j = 0; j <= I; j++)
{
System.out.print((char) (alphabet + j) + ” “);
}
System.out.println();
}
}
}

Output:
ABCDEF

ABCDE

ABCD

ABC

AB

AB

ABC

ABCD

ABCDE

ABCDEF
Import java.util.Scanner;

Public class MainClass


{
Public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

System.out.println(“How many rows you want in this pattern?”);

Int rows = sc.nextInt();

System.out.println(“Here is your pattern….!!!”);

For (int I = 1; I <= rows; i++)


{
For (int j = 1; j <= I; j++)
{
System.out.print(i+” “);
}

System.out.println();
}

//Close the resources

Sc.close();
}
}

Output
1
22
333
4444
55555
666666
7777777
Class Car{
Public Car()
{
System.out.println(“Class Car”);
}
Public void vehicleType()
{
System.out.println(“Vehicle Type: Car”);
}
}
Class Maruti extends Car{
Public Maruti()
{
System.out.println(“Class Maruti”);
}
Public void brand()
{
System.out.println(“Brand: Maruti”);
}
Public void speed()
{
System.out.println(“Max: 90Kmph”);
}
}
Public class Maruti800 extends Maruti{

Public Maruti800()
{
System.out.println(“Maruti Model: 800”);
}
Public void speed()
{
System.out.println(“Max: 80Kmph”);
}
Public static void main(String args[])
{
Maruti800 obj=new Maruti800();
Obj.vehicleType();
Obj.brand();
Obj.speed();
}
}

Output:

Class Car
Class Maruti
Maruti Model: 800
Vehicle Type: Car
Brand: Maruti
Max: 80Kmph
Class Lamp
{
// stores the value for light
// true if light is on
// false if light is off
Boolean isOn;
// method to turn on the light
Void turnOn() {
isOn = true;
System.out.println(“Light on? “ + isOn);
}
// method to turnoff the light
Void turnOff() {
isOn = false;
System.out.println(“Light on? “ + isOn);
}
}
Class Main
{
Public static void main(String[] args) {
// create objects led and halogen
Lamp led = new Lamp();
Lamp halogen = new Lamp();
// turn on the light by
// calling method turnOn()
Led.turnOn();
// turn off the light by
// calling method turnOff()
Halogen.turnOff();
}
}

Output:
Light on? True
Light on? false
Class Employee{
Int id;
String name;
Float salary;
Void insert(int I, String n, float s) {
Id=I;
Name=n;
Salary=s;
}
Void display(){System.out.println(id+” “+name+” “+salary);}
}
Public class TestEmployee {
Public static void main(String[] args) {
Employee e1=new Employee();
Employee e2=new Employee();
Employee e3=new Employee();
E1.insert(101,”ajeet”,45000);
E2.insert(102,”irfan”,25000);
E3.insert(103,”nakul”,55000);
E1.display();
E2.display();
E3.display();
}
}

Output:
101 ajeet 45000.0
102 irfan 25000.0
103 nakul 55000.0

You might also like