Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercises 9-0. Compile, execute, and test the programs in this chapter. 9-1. Rei

ID: 3738273 • Letter: E

Question

Exercises 9-0. Compile, execute, and test the programs in this chapter. 9-1. Reimplement the Student_info class so that it calculates the final grade when reading the student's record, and stores that grade in the object. Reimplement the grade function to use this precomputed value. If we define the name function as a plain, nonconst member function, what other functions in our system must change and why? Our grade function was written a Student_info object whose values had not yet been rea to catch this exception. Write a program that triggers the exception but does Write a program that catches the exception. 9-2. 9-3. to throw an exception if a user tried to calculate a grade for d. Users who care are expected not catch it does not 9-4. Rewrite your program from the previous exercise to use the valid function ereby avoid ing the exception altogether. 9-5. Write a class and associated functions to generate grade for pass/fail credit. Assume that only the midterm and final grades mattd st the students dent passes with an average exam score greater than 60. The report s take the course that a stu s for students who in alphabetical order, and indicate P or F as the grade. Rewrite the grading program for the pass/fail students dents so that the report shows all the stor 9-7. The read_hw function $4.1.3/57 solves a general roe be part of the instead, e implementation o 9-6. dents who passed, followed by all the students who failed. a vector) even though its name suggests that it shou Student_info. Of wanted to integrate it with the rest of the Student-iy? How not intended for public access despite its apparent generality es a general problem (reading a sequence of instead, that you course, we could change its name- but suppose, darify that if How would you do so? would you

Explanation / Answer

Solution 9-1

public void grade_func(String S)
{
System.out.print("Grade is: "+S);
}

Solution 9-2

Const keyword is used in functions if we donot want anychanges to occor in object data inside functions which is reflected back in calling function. With plain function object data is modifible so we need to change data accordingly with using const functions.

solution 9-3

That may be the case when we ask for roll number and serach for that record and do not find that record

import java.util.Scanner;

public class student_info
{

public static void main(String args[])
{
String [] name = new String[50];
String [] Roll =new String[50];
String [] Grade= new String[2]; // Grades being A for 90 to 100, B(80-90), C(60-80), D(40-60), E(0-40)
int marks[][] = new int[6][6];
int i,j, flag;
String r;
float total=0, avg;
Scanner scanner = new Scanner(System.in);
for (j=0;j<5;j++)
{
System.out.print("Enter Details of student #"+(j+1));
System.out.print("Enter Roll No: ");
Roll[j] = scanner.nextLine();   
System.out.print("Enter Name: ");
name[j] = scanner.nextLine();   
  
for(i=0; i<5; i++) {
System.out.print("Enter Marks "+(i+1)+":");
marks[j][i] = scanner.nextInt();

}
}
for(j=0;j<5;j++)
{
System.out.println("Enter Roll Number is to be searched: ");
r = scanner.nextLine();
flag=0;
for(i=0;i<5;i++)
{
if(r==Roll[i])
flag=1;
}
if( flag==0)
throw new NullPointerException("No Such Roll nUmber");

System.out.println("Roll Number is : "+ Roll[j]);
System.out.println("Name is : "+ name[j]);

total=0;
for(i=0;i<5;i++)
{
total=total+marks[j][i];
  
}
avg = total/5;
if(avg>=90)
{
Grade[j]="A";
}
else if(avg>=80 && avg<90)
{
Grade[j]="B";
}
else if(avg>=60 && avg<80)
{
Grade[j]="C";
}
else if(avg>=40 && avg<60)
{
Grade[j]="D";
}
else
{
Grade[j]="E";
}
}

  

scanner.close();
}   
}

// Program to catch exception

import java.util.Scanner;

public class student_info
{

public static void main(String args[])
{
String [] name = new String[50];
String [] Roll =new String[50];
String [] Grade= new String[2]; // Grades being A for 90 to 100, B(80-90), C(60-80), D(40-60), E(0-40)
int marks[][] = new int[6][6];
int i,j, flag;
String r;
float total=0, avg;
Scanner scanner = new Scanner(System.in);
for (j=0;j<5;j++)
{
System.out.print("Enter Details of student #"+(j+1));
System.out.print("Enter Roll No: ");
Roll[j] = scanner.nextLine();   
System.out.print("Enter Name: ");
name[j] = scanner.nextLine();   
  
for(i=0; i<5; i++) {
System.out.print("Enter Marks "+(i+1)+":");
marks[j][i] = scanner.nextInt();

}
}
for(j=0;j<5;j++)
{
System.out.println("Enter Roll Number is to be searched: ");
r = scanner.nextLine();
flag=0;
for(i=0;i<5;i++)
{
if(r==Roll[i])
flag=1;
}
try
{
  
if( flag==0)
{
throw new Exception("No Roll Number"); //throws an Exception if age is negative
}
}
catch(Exception e)
{
System.out.println(e); //Prints exception description
}
  
System.out.println("Roll Number is : "+ Roll[j]);
System.out.println("Name is : "+ name[j]);

total=0;
for(i=0;i<5;i++)
{
total=total+marks[j][i];
  
}
avg = total/5;
if(avg>=90)
{
Grade[j]="A";
}
else if(avg>=80 && avg<90)
{
Grade[j]="B";
}
else if(avg>=60 && avg<80)
{
Grade[j]="C";
}
else if(avg>=40 && avg<60)
{
Grade[j]="D";
}
else
{
Grade[j]="E";
}
}

  

scanner.close();
}   
}

Sol 9-4

Using Valid function

import java.util.Scanner;

public class student_info
{
public static int valid(String [] g)
{
int i, j, flag;
String r;
Scanner s = new Scanner(System.in);

System.out.println("Enter Roll Number is to be searched: ");
r = s.nextLine();
flag=0;
for(i=0;i<5;i++)
{
if(r==g[i])
flag=1;
}
return flag;
}
  

public static void main(String args[])
{
String [] name = new String[50];
String [] Roll =new String[50];
String [] Grade= new String[2]; // Grades being A for 90 to 100, B(80-90), C(60-80), D(40-60), E(0-40)
int marks[][] = new int[6][6];
int i,j, flag;
String r;
float total=0, avg;
Scanner scanner = new Scanner(System.in);
for (j=0;j<5;j++)
{
System.out.print("Enter Details of student #"+(j+1));
System.out.print("Enter Roll No: ");
Roll[j] = scanner.nextLine();   
System.out.print("Enter Name: ");
name[j] = scanner.nextLine();   
  
for(i=0; i<5; i++) {
System.out.print("Enter Marks "+(i+1)+":");
marks[j][i] = scanner.nextInt();

}
}
flag=valid(Roll);
if(flag==0)
System.exit(0);
for (j=0;j<5;j++)
{

System.out.println("Roll Number is : "+ Roll[j]);
System.out.println("Name is : "+ name[j]);

total=0;
for(i=0;i<5;i++)
{
total=total+marks[j][i];
  
}
avg = total/5;
if(avg>=90)
{
Grade[j]="A";
}
else if(avg>=80 && avg<90)
{
Grade[j]="B";
}
else if(avg>=60 && avg<80)
{
Grade[j]="C";
}
else if(avg>=40 && avg<60)
{
Grade[j]="D";
}
else
{
Grade[j]="E";
}
}

  

scanner.close();
}   
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote