Answer to Question #202392 in Java | JSP | JSF for Shema Derrick

Question #202392

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.


1
Expert's answer
2021-06-03T00:01:00-0400
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();
	}
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS