Computer project
Computer project
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.*;
if (num <= 1)
return false;
if (num % i == 0)
return false;
return true;
int a,b;
int n = sc.nextInt();
for(a=0;a<n;a++)
for(b=0;b<n;b++)
matrix[a][b]=sc.nextInt();
if (isPrime(primeNum))
matrix[i][i] = primeNum;
primeNum++;
if (compositeNum == primeNum)
compositeNum++;
matrix[i][n - 1 - i] = compositeNum;
compositeNum++;
System.out.println("Generated Matrix:");
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.
Code:-
import java.util.Scanner;
String s = sc.nextLine();
w1[i]=calculateWeight(w[i]);
{
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;
int netWeight=0;
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.
Code:-
// Superclass Worker
class Worker
// Instance variables
// Parameterized constructor
this.name = name;
this.basic = basic;
return basic;
// Subclass Wages
{
// Instance variables
// Parameterized constructor
this.hrs = hrs;
this.rate = rate;
@Override
double basicPay = getBasic(); // Use the getter method for basic pay
}
// Main class to test the implementation
worker.display();
Output:-
Code:-
class Record {
public Record()
{
mk = new int[50];
super();
index = 0;
// Method to find the index of the topmost rank without sorting the array
highestRank = mk[i];
index = i;
// Method to display the names and ranks along with the name having the topmost rank
@Override
super.display();
// Example usage
public class Main
rank.readvalues();
rank.highest();
rank.display();
Output:-
Code:-
// Stock class definition
class Stock
// Data members
String item;
int qt;
double rate;
double amt;
// Parameterized constructor
this.item = item;
this.qt = qt;
this.rate = rate;
void display()
System.out.println("Stock Details:");
int pqty;
double prate;
// Parameterized constructor
Purchase(String item, int qt, double rate, int pqty, double prate)
this.pqty = pqty;
this.prate = prate;
void update()
if (prate != rate)
@Override
void display()
System.out.println("\nAfter Purchase:");
}
// Main class to test the Stock and Purchase classes
Output:-
Variable Description Table(VDT):-