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

Computer project

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Computer project

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Q.

Write a program to accept the elements in a matrix of order n×n , where n is a even
number and performs the following task:
•fill all the left diagonals with prime numbers only
•fill all the right diagonals with composite numbers only

Code:-
import java.util.*;

public class DiagonalMatrix

public static boolean isPrime(int num)

if (num <= 1)

return false;

for (int i = 2; i <= Math.sqrt(num); i++)

if (num % i == 0)

return false;

return true;

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.print("Enter the size of the matrix (even number): ");

int a,b;

int n = sc.nextInt();

int[][] matrix = new int[n][n];


System.out.println("Elements present in the matrix=");

for(a=0;a<n;a++)

for(b=0;b<n;b++)

matrix[a][b]=sc.nextInt();

int primeNum = 2, compositeNum = 4;

for (int i = 0; i<n; i++)

if (isPrime(primeNum))

matrix[i][i] = primeNum;

primeNum++;

if (compositeNum == primeNum)

compositeNum++;

matrix[i][n - 1 - i] = compositeNum;

compositeNum++;

System.out.println("Generated Matrix:");

for (int i = 0; i < n; i++)

for (int j = 0; j < n; j++)

System.out.print(matrix[i][j] + " ");

System.out.println();

}
}

Output:-

Algorithm:-
Step 1:- Get the size of the square matrix and its elements from the user.

Step 2:- Identify prime and composite numbers in the matrix, and count them.

Step 3:- Check if there are enough prime and composite numbers to fill the diagonals.

Step 4:- Convert the prime and composite numbers into separate arrays.

Step 5:- Assign prime numbers to the principal diagonal and composite numbers to the secondary
diagonal, and print the modified matrix.

Variable Description Table(VDT):-


Variable name Data Type Purpose
N int To store size of square matrix.
num int Temporary variable to store
the value of n.
I int To iterate the loop.
Matrix int Array to store the elements of
the matrix.
IsPrime String To store prime no’s extracted
by the loop.
CompositeNum String To store Composite no’s
extracted by the loop.
Q. Write a program to accept a sentence and arrange the words in ascending order
according to the weightage calculated according to the potential addition of alphabets as
given below:
A-Z = 1-26
a-z = 27-52
0-9 = 53-62
Special character= 63

Code:-
import java.util.Scanner;

public class WordWeightage

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.print("Enter a sentence: ");

String s = sc.nextLine();

String[] w = s.split(" ");

int[] w1 = new int[w.length];

for (int i=0;i<w.length;i++)

w1[i]=calculateWeight(w[i]);

for (int i=0;i<w.length-1;i++)

for (int j=0;j< w.length-i-1;j++)

{
if (w1[j] > w1[j+1])

int t = w1[j];

w1[j] = w1[j+1];

w1[j+1] = t;

String tw = w[j];

w[j] = w[j+1];

w[j+1] = tw;

System.out.println("Words arranged in ascending order according to their netWeightage:");

for (String word : w)

System.out.print(word+ " ");

static int calculateWeight(String word)

int netWeight=0;

for (char c:word.toCharArray())

if (Character.isLetter(c))

if (Character.isUpperCase(c))

netWeight+=c-'A'+1;

else

netWeight+=c-'a'+27;
}

} else if (Character.isDigit(c))

netWeight+=c-'0'+53;

} else

netWeight+=63;

return netWeight;

Algorithm:-
Step 1: Take a sentence as input from the user.

Step 2: Split the sentence into individual words and calculate the weight of each word using the
`calculateWeight` function.

Step 3: Sort the words in ascending order based on their calculated weights using the bubble sort
algorithm.

Step 4: Swap the words and their corresponding weights during the sorting process.

Step 5: Print the sorted words in ascending order of their weights.


Variable Description Table(VDT):-
Variable name Data Type Purpose
s string To store the inputed string.
w string To store each word in the
inputed sentence
w1 int Stores the total weightage of
each word in the sentence.
i int To iterate the loop
j int To iterate the loop
t int For swapping weights during
sorting process.
Tw string For swapping words during
sorting process.
word string String parameter used in
‘calculateWeight’ method.
netweight int Integer variable used in
‘calculateWeight’ method.
c char Character variable used in
‘calculateWeight’ method.
Q. A superclass Worker has been defined to store the details of a worker. Define a
subclass Wages to compute the monthly wages for the worker. The details/specifications
of both the classes are given below:
Class name: Worker
Data Members/instance variables:
Name: to store the name of the worker
Basic: to store the basic pay in decimals
Member functions:
Worker (…): Parameterised constructor to assign values to the instance variables
void display (): display the worker’s details
Class name: Wages
Data Members/instance variables:
hrs: stores the hours worked
rate: stores rate per hour
wage: stores the overall wage of the worker
Member functions:
Wages (…): Parameterised constructor to assign values to the instance variables of both
the classes
double overtime (): Calculates and returns the overtime amount as (hours*rate)
void display (): Calculates the wage using the formula wage = overtime amount +
Basic pay and displays it along with the other details Specify the class Worker giving
details of the constructor () and void display ( ). Using the concept of inheritance, specify
the class Wages giving details of constructor ( ), double-overtime () and void display ().

Code:-
// Superclass Worker

class Worker

// Instance variables

private String name;

private double basic;

// Parameterized constructor

public Worker(String name, double basic)

this.name = name;

this.basic = basic;

// Method to display worker's details

public void display()

System.out.println("Worker's Name: " + name);

System.out.println("Basic Pay: " + basic);

// Getter method for basic pay

public double getBasic()

return basic;

// Subclass Wages

class Wages extends Worker

{
// Instance variables

private int hrs;

private double rate;

private double wage;

// Parameterized constructor

public Wages(String name, double basic, int hrs, double rate)

super(name, basic); // Call to superclass constructor

this.hrs = hrs;

this.rate = rate;

// Method to calculate and return overtime amount

public double overtime()

return hrs * rate;

// Method to calculate wage and display all details

@Override

public void display()

double basicPay = getBasic(); // Use the getter method for basic pay

wage = overtime() + basicPay;

super.display(); // Call to superclass display method

System.out.println("Hours Worked: " + hrs);

System.out.println("Rate per Hour: " + rate);

System.out.println("Total Wage: " + wage);

}
// Main class to test the implementation

public class Main {

public static void main(String[] args)

// Creating an instance of Wages class

Wages worker = new Wages("John Doe", 1500.00, 10, 50.00);

// Display the worker's details

worker.display();

Output:-

Variable Description Table(VDT):-

Variable Name Data Type Purpose


name string Stores the name of the worker.
basic double Stores the basic pay of the
worker.
hrs int Stores the no of hours worked
by the worker.
rate double Stores the rate per hour for
overtime work.
wage double Stores the total pay which
includes basic pay and
overtime pay.
worker wages Refers to an instance of the
wages class,representating a
worker named “John Doe”.
basic pay double Stores the basic pay fetched
using the getBasic method in
the display method of Wages.
Q. A superclass Record has been defined to store the names and ranks of 50 students.
Define a sub-class Rank to find the highest rank along with the name. The details of both
classes are given below:
Class name: Record
Data members/instance variables:
name[ ]: to store the names of students
mk[ ]: to store the ranks of students
Member functions:
Record(): constructor to initialize data members
void readvalues(): to store the names and ranks
void display(): displays the names and the corresponding ranks

Class name: Rank


Data members/instance variables:
index: integer to store the index of the topmost rank
Member functions:
Rank( ): constructor to invoke the base class constructor and to initialize index = 0
void highest(): finds the index/location of the topmost rank and stores it in the index
without sorting the array.
void display(): displays the names and rank along with the name haring the topmost rank.
Specific the class Record giving details of the constructor(), void readvalues( ) and void
display (). Using the concept of inheritance, specify the class Rank giving details of
constructor ( ), void highest() and void display().

Code:-
class Record {

// Data members/instance variables

protected String[] name;

protected int[] mk;

// Constructor to initialize data members

public Record()
{

name = new String[50];

mk = new int[50];

// Method to store the names and ranks

public void readvalues()

// Assuming a hypothetical way of reading values for demonstration purposes

for (int i = 0; i < 50; i++)

name[i] = "Student" + (i + 1);

mk[i] = (int) (Math.random() * 100) + 1; // Random ranks between 1 and 100

// Method to display the names and corresponding ranks

public void display()

for (int i = 0; i < 50; i++) {

System.out.println("Name: " + name[i] + ", Rank: " + mk[i]);

class Rank extends Record

// Data member to store the index of the topmost rank

private int index;

// Constructor to invoke the base class constructor and initialize index = 0


public Rank()

super();

index = 0;

// Method to find the index of the topmost rank without sorting the array

public void highest()

int highestRank = mk[0];

for (int i = 1; i < mk.length; i++)

if (mk[i] < highestRank)

highestRank = mk[i];

index = i;

// Method to display the names and ranks along with the name having the topmost rank

@Override

public void display()

super.display();

System.out.println("\nStudent with the highest rank:");

System.out.println("Name: " + name[index] + ", Rank: " + mk[index]);

// Example usage
public class Main

public static void main(String[] args)

Rank rank = new Rank();

rank.readvalues();

rank.highest();

rank.display();

Output:-

Variable Description Table(VDT):-

Variable Name Data Type Purpose


Record A base class representing a
record of student names and
their ranks.
name String[] An array of String in Record
class to store student names.
Initialized with 50 elements.
mk Int[] An array of int in Record
class to store student ranks.
Initialized with 50 elements.
index int An int variable in Rank class
to store the index of the
student with the highest rank.
Initialized to 0.
i int Variable used for for loop
iteration
Q. A superclass Stock has been defined to store the details of the stock of a retail store.
Define a subclass Purchase to store the details of the items purchased with the new rate
and updates the stock. Some of the members of the classes are given below: [10]
Class name: Stock
Data members/instance variables:
item: to store the name of the item
qt: to store the quantity of an item in stock
rate: to store the unit price of an item
amt: to store the net value of the item in stock
Member functions:
Stock (…): parameterized constructor to assign values to the data members
void display(): to display the stock details
Class name: Purchase
Data members/instance variables:
pqty: to store the purchased quantity
prate: to store the unit price of the purchased item
Member functions/ methods:
Purchase(…): parameterized constructor to assign values to the data members of both
classes
void update (): to update stock by adding the previous quantity by the purchased quantity
and replace the rate of the item if there is a difference in the purchase rate. Also, update
the current stock value as (quantity * unit price)
void display(): to display the stock details before and after updating. Specify the class
Stock, giving details of the constructor() and void display().

Code:-
// Stock class definition

class Stock

// Data members

String item;

int qt;
double rate;

double amt;

// Parameterized constructor

Stock(String item, int qt, double rate)

this.item = item;

this.qt = qt;

this.rate = rate;

this.amt = qt * rate; // Calculate the net value

// Method to display stock details

void display()

System.out.println("Stock Details:");

System.out.println("Item: " + item);

System.out.println("Quantity: " + qt);

System.out.println("Rate: $" + rate);

System.out.println("Net Value: $" + amt);

// Purchase class definition (subclass of Stock)

class Purchase extends Stock

// Additional data members

int pqty;

double prate;

// Parameterized constructor
Purchase(String item, int qt, double rate, int pqty, double prate)

super(item, qt, rate);

this.pqty = pqty;

this.prate = prate;

// Method to update stock details based on purchase

void update()

// Update quantity and rate if there is a difference in purchase rate

if (prate != rate)

rate = prate; // Update rate to purchase rate

qt += pqty; // Add purchased quantity to existing quantity

amt = qt * rate; // Update net value

// Method to display stock details before and after updating

@Override

void display()

super.display(); // Display original stock details

System.out.println("\nAfter Purchase:");

System.out.println("Updated Quantity: " + qt);

System.out.println("Updated Rate: $" + rate);

System.out.println("Updated Net Value: $" + amt);

}
// Main class to test the Stock and Purchase classes

public class Main

public static void main(String[] args)

// Create an instance of Stock

Stock stockItem = new Stock("Laptop", 10, 800.0);

stockItem.display(); // Display initial stock details

// Create an instance of Purchase

Purchase purchaseItem = new Purchase("Laptop", 10, 800.0, 5, 850.0);

purchaseItem.update(); // Update stock based on purchase

purchaseItem.display(); // Display updated stock details

Output:-
Variable Description Table(VDT):-

Variable Name Data Type Purpose


item string Name of the stock item.
qt int Quantity of the stock item.
rate double Rate of each unit of the stock
itam.
amt double Net value of the stock item .
pqty int Quantity of the item
purchased.
prate double Rate of each unit of the
purchased item.

You might also like