Question

In: Computer Science

Hello, Please write this program in java and include a lot of comments and please try...

Hello,

Please write this program in java and include a lot of comments and please try to make it as simple/descriptive as possible since I am also learning how to code. The instructions the professor gave was:

Create your own class

  • Your own class can be anything you want

  • Must have 3 instance variables

  • 1 constructor variable → set the instance variables

  • 1 method that does something useful in relation to the class

  • Create a driver class that creates an object ( the object is referred to the method above)

  • Call the method that I created using the object

An example she gave was using like a student class because it has many variables such as grades, classification, major, etc.

Solutions

Expert Solution

Implementation In JAVA:

import java.util.ArrayList;
import java.util.Scanner;

//Driver class
public class Student_Driver {

//   main method or driver method
   public static void main(String[] args) {
      
//       list consists of Students
       ArrayList<Students> list= new ArrayList<Students>();
      
//   create instance of Scanner class for taking inputs from users  
       Scanner s= new Scanner(System.in);
      
       System.out.println("Enter the Details of 5 Students of class");
      
       System.out.println();
      
//       take details of 5 students so looping untill 5
       for(int i=0;i<5;i++) {
          
           System.out.println("Enter the details of "+(i+1)+"th Student : ");
          
//           take details from user as input
           System.out.print("Enter the Name of Student : ");
           String name=s.next();
          
           System.out.print("Enter the Roll no of Student : ");
           int roll=s.nextInt();
          
           System.out.print("Enter the Grade of Student : ");
           double grade=s.nextDouble();
          
//           create instance of Students class and pass arguments
//           in Constructor of Students class
           Students st= new Students(name,roll,grade);
          
//           add instance of Students class in List for future refrence
           list.add(st);
          
           System.out.println();
          
       }
      
//       showing details of Students
       System.out.println("Student of whole class with details : ");
      
//       will print details of Students
       for(int i=0;i<list.size();i++) {
          
//           Store instance/object of Student class and
           Students st = list.get(i);
//           and call the function in Students class for printing details
           st.to_String();
           System.out.println();
          
       }
      
System.out.println();
  
// will call the function average_grades_of_whole_class from driver class
System.out.println("The Average grades of whole class is "+average_grades_of_whole_class(list));
      
      
   }
  
//   made static because directly access into main
   public static double average_grades_of_whole_class(ArrayList<Students> list) {
      
//       will store sum of all grades
       double sum=0;
      
//       loop untill list.size
       for(int i=0;i<list.size();i++) {
          
           Students st = list.get(i);
//           call getgrade for every student and store in sum
           sum+=st.getgrade();
          
       }
//       getting average
       double avg=sum/list.size();
      
//       return avg.
       return avg;
   }

}

// class Student
class Students{
  
//   Instance variables declared private so
//   no one from outside access them
   private String name;
   private int roll_no;
   private double grade;
  
//   3 argument constructor
   public Students(String name,int roll_no,double grade) {
      
//       initialize variables
       this.name=name;
       this.roll_no=roll_no;
       this.grade=grade;
      
   }
  
//   now below are getters and setters through which we can access those variables
  
//   will return name
   public String getname() {
      
       return name;
   }
  
//   will set name
public void setname(String name) {
      
       this.name= name;
   }
  
// will return roll.no
public int getroll() {
  
   return roll_no;
}
  
// will set roll no.
public void setroll(int roll_no) {
       this.roll_no=roll_no;
   }
  
// will return grade
public double getgrade() {
   return grade;
}
  
// will set grades
   public void setgrades(double grade) {
      
       this.grade=grade;
      
   }
  
  
//   method that does something useful in relation to the class
//   this will print the details of Student
  
   public void to_String() {
      
       System.out.println("Name : "+name );
       System.out.println("Roll no. : "+roll_no );
       System.out.println("Grade : "+grade );
      
   }

  
}

SAMPLE OUTPUT:

If you have any doubt regarding this question please ask mein comments

// THANK YOU:-)


Related Solutions

Write a complete JAVA program, including comments (worth 2 pts -- include a good comment at...
Write a complete JAVA program, including comments (worth 2 pts -- include a good comment at the top and at least one more good comment later in the program), to process data for the registrar as follows: NOTE: Include prompts. Send all output to the screen. 1. Read in the id number of a student, the number of credits the student has, and the student’s grade point average (this is a number like 3.25). Print the original data right after...
WRITE IN JAVA, please include comments and explain what you did A Book has such properties...
WRITE IN JAVA, please include comments and explain what you did A Book has such properties as title, author, and numberOfPages. A Volume will have properties such as volumeName, numberOfBooks, and an array of book objects (Book [ ]). You are required to develop the Book and Volume classes, then write an application (DemoVolume) to test your classes. The directions below will give assistance. Create a class called Book with the following properties using appropriate data types: Title, Author, numberOfPages,...
Please use Java language with comments! Thanks! Write a program that will display multiple dots move...
Please use Java language with comments! Thanks! Write a program that will display multiple dots move across the Frame and randomly change direction when they hit the edge of the screen. Do this by creating a Dot class and making each dot an object of that class. You may reuse code written in class for this part of the assignment. Create a subclass of the Dot class called PlayerDot that is controlled by the player through keyboard input (arrow keys)...
Please include comments on what you are doing.   Using linked lists, write a Python program that...
Please include comments on what you are doing.   Using linked lists, write a Python program that performs the following tasks: store the records for each college found in the input file - colleges.csv - into a linked list. (File includes name and state data fields) allow the user to search the linked list for a college’s name; display a message indicating whether or not the college’s name was in the database allow the user to enter a state's name and...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three int||eger values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write the following program in java please. a. Include a good comment when you write the...
Write the following program in java please. a. Include a good comment when you write the method described below: Method method1 receives an integer, n, and a real number, d, and returns a real number. If the integer is positive it returns the square root of the real number added to the integer. If the integer is negative or zero it returns the absolute value of the integer multiplied by the real number. b. Write the statements from the main...
Please show solution and comments for this data structure using java.​ Implement a program in Java...
Please show solution and comments for this data structure using java.​ Implement a program in Java to convert an infix expression that includes (, ), +, -, *,     and / to postfix expression. For simplicity, your program will read from standard input (until the user enters the symbol “=”) an infix expression of single lower case and the operators +, -, /, *, and ( ), and output a postfix expression.
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT