Using java, define a class student with the following specification Private members of class student: admno integer sname string eng, math, science of type float total of type float ctotal() //a function to calculate eng + math + science with float return type. Public member function of class student: Takedata() //Function to accept values for admno, sname, eng, science and invoke ctotal() to calculate total. Showdata() //Function to display all the data members on the screen.
import java.util.Scanner;
class Student {
// Private members of class student: admno integer sname string eng, math,
// science of type float total of type float
private int admno;
private String sname;
private float eng;
private float math;
private float science;
private float total;
/**
* a function to calculate eng + math + science with float return type.
*
* @return
*/
private float ctotal() {
return eng + math + science;
}
// Public member function of class student:
/***
* Takedata() Function to accept values for admno, sname, eng, science and
* invoke ctotal() to calculate total.
*
* @param admno
* @param sname
* @param eng
* @param math
* @param science
*/
public void Takedata(int admno, String sname, float eng, float math, float science) {
this.admno = admno;
this.sname = sname;
this.eng = eng;
this.math = math;
this.science = science;
this.total = ctotal();
}
/**
* Showdata() - Function to display all the data members on the screen.
*/
public void Showdata() {
System.out.println("Admno: " + admno);
System.out.println("Name: " + sname);
System.out.println("Eng: " + eng);
System.out.println("Math: " + math);
System.out.println("Science: " + science);
System.out.println("Total: " + total);
}
}
public class Q202392 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Student newStudent = new Student();
int admno;
String sname;
float eng;
float math;
float science;
System.out.print("Enter admno: ");
admno = input.nextInt();
input.nextLine();
System.out.print("Enter sname: ");
sname = input.nextLine();
System.out.print("Enter eng: ");
eng = input.nextFloat();
System.out.print("Enter math: ");
math = input.nextFloat();
System.out.print("Enter science: ");
science = input.nextFloat();
newStudent.Takedata(admno, sname, eng, math, science);
newStudent.Showdata();
input.close();
}
}
Comments
Leave a comment