slip.10

1.
import java.util.Scanner;

@FunctionalInterface
interface CubeCalculator {
 int cube(int n);
}
public class CubeFunctionalInterface {
 public static void main(String[] args) {
 Scanner sc = new Scanner(System.in);
 System.out.print("Enter a number: ");
 int num = sc.nextInt();

 CubeCalculator cubeCalc = n -> n * n * n;
 int result = cubeCalc.cube(num);
 System.out.println("Cube of " + num + " is: " + result);
 sc.close();
 }
}

2.
Step 1: Create package student
File: StudentInfo.java

package student;
public class StudentInfo {
 private int rollNo;
 private String name;
 private String className;
 private double percentage;
 
 public StudentInfo(int rollNo, String name, String className, double
percentage) {
 this.rollNo = rollNo;
 this.name = name;
 this.className = className;
 this.percentage = percentage;
 }
 
 public void display() {
 System.out.println("\n--- Student Information ---");
 System.out.println("Roll No : " + rollNo);
 System.out.println("Name : " + name);
 System.out.println("Class : " + className);
 System.out.println("Percentage : " + percentage + "%");
 }
}
File: StudentPer.java
package student;
public class StudentPer {
  
 public static double calculatePercentage(double[] marks) {

double total = 0;
 for (double m : marks) {
 total += m;
 }
 return total / marks.length;
 }
}
Step 2: Driver Class (outside the package)
File: StudentMain.java
import student.StudentInfo;
import student.StudentPer;
import java.util.Scanner;
public class StudentMain {
 public static void main(String[] args) {
 Scanner sc = new Scanner(System.in);
 System.out.print("Enter Roll No: ");
 int rollNo = sc.nextInt();
 sc.nextLine(); 
 System.out.print("Enter Name: ");
 String name = sc.nextLine();
 System.out.print("Enter Class: ");
 String className = sc.nextLine();
 double[] marks = new double[6];
 System.out.println("Enter marks of 6 subjects: ");
 for (int i = 0; i < 6; i++) {
 System.out.print("Subject " + (i + 1) + ": ");
 marks[i] = sc.nextDouble();
 }
 double percentage = StudentPer.calculatePercentage(marks);
 StudentInfo student = new StudentInfo(rollNo, name, className,
percentage);
 student.display();
sc.close();
 }
}

Comments

Popular posts from this blog

slip 25

slip.1

slip.14