Week 5: Java – Arrays & OOP Applications
Q1. Find Second Largest Element in an Array
Objective: Read n integers and find the second largest value.
import java.util.Scanner;
public class P1 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
if (n < 2) {
System.out.println("Error: Array must have at least 2 elements.");
return;
}
int[] arr = new int[n];
System.out.println("Enter " + n + " elements: ");
for(int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int firstLargest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
if (arr[i] > firstLargest) {
secondLargest = firstLargest;
firstLargest = arr[i];
} else if (arr[i] > secondLargest && arr[i] < firstLargest) {
secondLargest = arr[i];
}
}
System.out.println("Second largest element: " + secondLargest);
sc.close();
}
}
Key Points:
- Initialize
firstLargestandsecondLargestwithInteger.MIN_VALUE. - Loop through the array and update the largest and second largest accordingly.
- Handles arrays with fewer than 2 elements.
Q2. Showroom Article with Maximum Loss
Objective: Create Article objects with cost and selling prices, calculate loss percentage, and find the article with the highest loss.
import java.util.Scanner;
class Article {
String name;
float costPrice;
float sellingPrice;
float lossPer;
void calc_loss_per() {
if (costPrice > 0) {
lossPer = ((costPrice - sellingPrice) / costPrice) * 100;
} else {
lossPer = 0;
}
}
void display() {
System.out.println("Article Name: " + name);
System.out.println("Cost Price: " + costPrice);
System.out.println("Selling Price: " + sellingPrice);
System.out.println("Loss Percentage: " + lossPer + "%");
}
static void find_high_loss(Article[] articles) {
int index = 0;
float maxLoss = articles[0].lossPer;
for (int i = 1; i < articles.length; i++) {
if (articles[i].lossPer > maxLoss) {
maxLoss = articles[i].lossPer;
index = i;
}
}
System.out.println("\nArticle with Highest Loss Percentage:");
articles[index].display();
}
}
public class Showroom {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of articles: ");
int n = sc.nextInt();
sc.nextLine(); // consume newline
Article[] articles = new Article[n];
for (int i = 0; i < n; i++) {
articles[i] = new Article();
System.out.println("\nEnter details for Article " + (i + 1) + ":");
System.out.print("Name: ");
articles[i].name = sc.nextLine();
System.out.print("Cost Price: ");
articles[i].costPrice = sc.nextFloat();
System.out.print("Selling Price: ");
articles[i].sellingPrice = sc.nextFloat();
sc.nextLine(); // consume newline
articles[i].calc_loss_per();
}
Article.find_high_loss(articles);
sc.close();
}
}
Key Points:
- Use array of objects (
Article[]) to store multiple articles. calc_loss_per()computes loss percentage for each article.find_high_loss()identifies and displays the article with maximum loss.- Handles user input carefully with
sc.nextLine()to avoid skipping inputs.
Week 5 Notes:
- Arrays can store primitive types or objects.
- Use
Integer.MIN_VALUEwhen searching for largest values in arrays. - Object arrays allow methods to operate collectively (like
find_high_loss()). - Proper input handling is important when mixing
nextLine()andnextInt()/nextFloat().