java 7-12
java 7-12
Design a class that can be used by a health care professional to keep track of a patient’s vital
statistics. Here’s what the class should do:
1. Construct a class called Patient
2. Store a String name for the patient
3. Store weight and height for patient as doubles
4. Construct a new patient using these values
5. Write a method called BMI which returns the patient’s BMI as a double. BMI can be
calculated as BMI = (Weight in Pounds / ( Height in inches x Height in inches ) ) x 703
6. Next, construct a class called “Patients” and create a main method.
Create a Patient object and assign some height and weight to that object. Display the BMI of
that patient.
Patient.java
package com.nhce.program1a;
public class Patient
{
String patient_name;
double weight;
double height;
Patients.java
package com.nhce.program1a;
public class Patients {
1. A static method called powerInt(int num1,int num2) that accepts two integers and returns
num1 to the power of num2 (num1 power num2).
2. A static method called powerDouble(double num1,int num2) that accepts one double and
one integer and returns num1 to the power of num2 (num1 power num2).
3. Call your method from another class without instantiating the class (i.e. call it like
Calculator.powerInt(12,10) since your methods are defined to be static).
Hint: Use Math.pow(double,double) to calculate the power.
package lab8;
import java.lang.Math;
class calp{
// driver code
public static void main(String args[])
{
int w;
w= powerInt(2,3);
System.out.println("CASE - 1 :");
System.out.println("static method called that accepts two integers ");
System.out.println(w);
double w1;
w1 = powerdouble(2.2,3);
System.out.println("CASE-2 :");
System.out.println("static method called that accepts one double and one
integer ");
System.out.println(w1);
System.out.println("CASE-3 :");
System.out.println("static method called from other class");
System.out.println(calp.powInt_withinotherclass(4,3));
}
}
Program 9.
Write a Program to take care of Number Format Exception if user enters values other than
integer for calculating average marks of 2 students. The name of the students and marks in 3
subjects are taken from the user while executing the program. In the same Program write your
own Exception classes to take care of Negative values and values out of range (i.e.
other than in the range of 0-100). Include finally to output the statement “Program
terminated”.
InputExceptionHandle.java
package com.nhce.program2a;
import java.util.Scanner;
avg_marks=(sum1+sum2)/6;
System.out.println("The average marks of "+ student1_name + " and " + student2_name + "
is " + avg_marks);
} msg
catch(NumberFormatException e)
{ System.out.println("Entered input is not a valid format for an integer" + e); }
catch(MarksValidationException e)
{ System.out.println(e.toString()); }
finally
{
in.close();
System.out.println("Program terminated!!!");
}
}
}
MarksValidationException.java
package com.nhce.program2a;
public class MarksValidationException extends Exception {
String msg;
MarksValidationException(String msg){
this.msg=msg;
}
Output
Enter Student1 Name:
vandana
Enter Student1 mark1 :
Fdgdghd
Entered input is not a valid format for an integer
-12
Enter Student1 mark2 :
105
Marks entered are negative or outside the range (0-100)
Program terminated!!!
Program 10
Create class of SalesPersons as a thread that will display fives sales persons name. Create a
class as Days as other Thread that has array of seven days. Call the instance of SalesPersons
in Days and start both the Threads. Suspend SalesPersons on Sunday and resume on the day
Wednesday.
Days.java
package com.nhce.program3a;
for(int i=0;i<7;i++)
{
if(weekDays[i].equals("Sunday"))
if(weekDays[i].equals("Wednesday"))
{
sales.resume();
System.out.println("resuming sales thread");
}
}
SalesPersons.java
package com.nhce.program3a;
public class SalesPersons extends Thread {
import java.util.HashMap;
// Constructor
public StudentAttendanceManagementSystem() {
attendanceMap.put(name, attendance);
return attendanceMap.get(name);
return attendanceMap.containsKey(name);
return attendanceMap.containsValue(attendance);
if (attendanceMap.containsKey(name)) {
attendanceMap.put(name, newAttendance);
} else {
// Add students
sams.addStudent("Alice", 5);
sams.addStudent("Bob", 3);
sams.addStudent("Charlie", 4);
// Retrieve attendance
sams.replaceAttendance("Alice", 6);
Program 12
System.out.println(result);
int count = 0;
int n = arr.length;
if (diff == indexSum) {
count++;
return count;
The countPairs method takes an array as input and iterates over all pairs of indices (i, j) where
1 <= i < j <= N. For each pair, it calculates the difference between the array elements at those
indices and the sum of the squares of the indices. If the difference is equal to the index sum, it
increments the count. In the main method, we provide the example array [4, 9, 6, 29, 30] and
print the result.