slip.14
1.
import java.util.Scanner;
class ZeroException extends Exception {
public ZeroException(String message) {
super(message);
}
}
public class PrimeCheck {
public static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
try {
if (num == 0) {
throw new ZeroException("Number is 0");
}
if (isPrime(num)) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
} catch (ZeroException e) {
System.out.println("Exception: " + e.getMessage());
} finally {
sc.close();
}
}
}
2.
Step 1: Package SY
File: SY/SYMarks.java
package SY;
public class SYMarks {
public double computerTotal;
public double mathsTotal;
public double electronicsTotal;
public SYMarks(double computerTotal, double mathsTotal, double
electronicsTotal) {
this.computerTotal = computerTotal;
this.mathsTotal = mathsTotal;
this.electronicsTotal = electronicsTotal;
}
}
Step 2: Package TY
File: TY/TYMarks.java
package TY;
public class TYMarks {
public double theory;
public double practicals;
public TYMarks(double theory, double practicals) {
this.theory = theory;
this.practicals = practicals;
}
}
Step 3: Student Class
File: Student.java
import SY.SYMarks;
import TY.TYMarks;
public class Student {
private int rollNumber;
private String name;
private SYMarks syMarks;
private TYMarks tyMarks;
public Student(int rollNumber, String name, SYMarks syMarks, TYMarks
tyMarks) {
this.rollNumber = rollNumber;
this.name = name;
this.syMarks = syMarks;
this.tyMarks = tyMarks;
}
public double getComputerTotal() {
return syMarks.computerTotal + tyMarks.theory + tyMarks.practicals;
}
public String getGrade() {
double total = getComputerTotal();
if (total >= 70) return "A";
else if (total >= 60) return "B";
else if (total >= 50) return "C";
else if (total >= 40) return "Pass Class";
else return "FAIL";
}
public void display() {
System.out.println("\n--- Student Result ---");
System.out.println("Roll Number : " + rollNumber);
System.out.println("Name : " + name);
System.out.println("SY Computer : " + syMarks.computerTotal);
System.out.println("TY Theory : " + tyMarks.theory);
System.out.println("TY Practical: " + tyMarks.practicals);
System.out.println("Total Marks : " + getComputerTotal());
System.out.println("Grade : " + getGrade());
}
}
Step 4: Driver Class
File: StudentDriver.java
import java.util.Scanner;
import SY.SYMarks;
import TY.TYMarks;
public class StudentDriver {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = sc.nextInt();
sc.nextLine(); // consume newline
Student[] students = new Student[n];
for (int i = 0; i < n; i++) {
System.out.println("\nEnter details for student " + (i + 1) + ":");
System.out.print("Roll Number: ");
int roll = sc.nextInt();
sc.nextLine();
System.out.print("Name : ");
String name = sc.nextLine();
System.out.print("SY Computer Marks : ");
double syComputer = sc.nextDouble();
System.out.print("SY Maths Marks : ");
double syMaths = sc.nextDouble();
System.out.print("SY Electronics Marks: ");
double syElectronics = sc.nextDouble();
System.out.print("TY Theory Marks : ");
double tyTheory = sc.nextDouble();
System.out.print("TY Practical Marks : ");
double tyPractical = sc.nextDouble();
sc.nextLine();
SYMarks syMarks = new SYMarks(syComputer, syMaths,
syElectronics);
TYMarks tyMarks = new TYMarks(tyTheory, tyPractical);
students[i] = new Student(roll, name, syMarks, tyMarks);
}
for (Student s : students) {
s.display();
}
sc.close();
}
}
Comments
Post a Comment