Question

In: Computer Science

write Java program has two classes ,( using Inheritance ) first class set ( id ,...

write Java program has two classes ,( using Inheritance )

first class set ( id , name ) and method output

second class ( id , name , Mark 1 , Mark 2 , Mark 3 )

method total , average , grade , results ( if the student pass or not ) , and output method

Solutions

Expert Solution

The program is as follows:

/******************************************************************************

                            Online Java Compiler.
                Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/

import java.util.*;

class Student
{
    int id;
    String name;
  
    public void setName(String name) //setter method for name
    {
        this.name=name;
    }
  
    public void setID(int id) //setter method for id
    {
        this.id=id;
    }
  
    public void getDetails() // method to retrieve the student details
    {
        System.out.println("*******Student Details*******");
        System.out.println("Student ID = "+id);
        System.out.println("Student Name = " +name);
    }
}

class StudentMarks extends Student
{
    float mark1;
    float mark2;
    float mark3;
  
    public void setMarks(float m1,float m2,float m3)
    {
        mark1=m1;
        mark2=m2;
        mark3=m3;
    }
  
    public float total()
    {
        return mark1+mark2+mark3;
    }
  
    public float average()
    {
        float avg= (mark1+mark2+mark3)/3;
        return avg;
    }
  
    public void grade() // grading is done individually for each subject
    {
        int i=1;
        float marks = 0;
        while(i<=3)
        {
            System.out.print("Subject " + i + ": ");
            if (i==1)
            {
                marks = mark1;
            }
            else if (i==2)
            {
                marks = mark2;
            }
            else
            {
                marks = mark3;
            }
            if(marks<40)
            {
                System.out.println("Grade F");
            }
            else if(marks>=40 && marks<60)
            {
                System.out.println("Grade D");
            }
            else if(marks>=60 && marks<70)
            {
                System.out.println("Grade C");
            }
            else if(marks>=70 && marks<80)
            {
                System.out.println("Grade B");
            }
            else if(marks>=80 && marks<90)
            {
                System.out.println("Grade A");
            }
            else
            {
                System.out.println("Grade A+");
            }
            i++;
        }
    }
  
    public void results() // results is displayed individually for each subject
    {
        if(mark1 < 40)
        {
            System.out.println("Subject 1: Fail");
        }
        else
            System.out.println("Subject 1: Pass");
          
        if(mark2 < 40)
        {
            System.out.println("Subject 2: Fail");
        }
        else
            System.out.println("Subject 2: Pass");
          
        if(mark3 < 40)
        {
            System.out.println("Subject 3: Fail");
        }
        else
            System.out.println("Subject 3: Pass");
    }
  
    public void getDetails()
    {
        super.getDetails(); // calling the base class getDetails() function to print student name and id
        System.out.println("Subject 1 Marks: " +mark1);
        System.out.println("Subject 2 Marks: " +mark2);
        System.out.println("Subject 3 Marks: " +mark3);
    }
}

public class Main
{
   public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        float m1,m2,m3;
        StudentMarks s1 = new StudentMarks();
        System.out.println("Enter Student Name: ");
        String name=sc.nextLine();
        s1.setName(name);
        System.out.println("Enter Student ID: ");
        int id=sc.nextInt();
        s1.setID(id);
        System.out.println("Enter Student Marks in 3 subjects: ");
        m1=sc.nextFloat();
        m2=sc.nextFloat();
        m3=sc.nextFloat();
        while(m1<0 || m1>100 || m2<0 || m2>100 || m3<0 || m3>100){ // if marj=ks is less than 0 or greater than 100, it has to be renered
            System.out.println("Enter valid marks!!!");
            m1=sc.nextFloat();
            m2=sc.nextFloat();
            m3=sc.nextFloat();
        }
        s1.setMarks(m1,m2,m3);
        s1.getDetails();
        float total= s1.total();
        System.out.println("Total marks = " + total + "/300");
        float avg = s1.average();
        System.out.println("Average marks = " + avg);
        s1.grade();
        s1.results();
   }
}

Output:


Related Solutions

Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
Developing a set of classes demonstrating inheritance - in class work
Developing a set of classes demonstrating inheritance - in class work
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Write a java program using the following information Design a LandTract class that has two fields...
Write a java program using the following information Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a double), and one for the width (a double). The class should have:  a constructor that accepts arguments for the two fields  a method that returns the tract’s area(i.e. length * width)  an equals method that accepts a LandTract object as an argument. If the argument object holds the same data (i.e. length and...
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has a name property (string) Has a speech property (string) Has a constructor that takes two arguments, the name and the speech It has getSpeech() and setSpeech(speech) It has getName and setName(name) It has a method speak() that prints the name of the animal and the speech. o Example output: Tom says meow. Mouse Inherits the Animal class Has a constructor takes a name and...
Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT