Click here to Skip to main content
16,004,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My output is as follows :
Enter the id
9999
Enter the name
tarak
Enter no of subjects
3
Enter the marks
88
Enter the marks
99
Enter the marks
77
Id :9999
Name :tarak
average :0.0
grade :F

What I have tried:

Java
package myproject;
import java.util.*;

class Student{
	
	int id;
	String name;
	int sub;
	int marks[] = new int[100];
	double avg;
	char grade;
	int i;
	
	public int getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public int getSub() {
		return sub;
	}
	
	public void setId(int id) {
		this.id=id;
	}
	public void setName(String name) {
		this.name=name;
	}
	public void setSub(int sub) {
		this.sub=sub;
	}
	
	public double getAvg() {
		return avg;
	}
	public void setAvg(double avg) {
		this.avg=avg;
	}
	
	public int[] getMarks() {
	return marks;
	}
	public void setMarks(int a[]) {
		this.marks=a;
	}
	
	public char getGrade() {
		return grade;
	}
	public void setGrade(char grade) {
		this.grade=grade;
	}
	public void calcAvg() {
		int total=0;
		for(int i=0; i<marks.length; i++) {
			total= total + marks[i];
		}
		avg=(total/marks.length);
	}
	
	public void findGrade() {
		
		if(marks[i]<50) {
			grade = 'F';
		}
		else {
			if(avg>=80 && avg<=100)
				grade='O';
			if(avg>=50 && avg<=80)
				grade='A';
			if(avg<50)
				grade='F';
		}
	}
}

public class Methods8 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Enter the id ");
		int id = sc.nextInt();
		System.out.println("Enter the name ");
		String name = sc.next();
		System.out.println("Enter no of subjects ");
		int sub = sc.nextInt();
		
		int a[] = new int[sub];
		
		for(int i=0; i<sub; i++) {
			System.out.println("Enter the marks ");
			a[i]=sc.nextInt();
		}
		
		Student s = new Student();
		s.setId(id);
		s.setName(name);
		s.setSub(sub);
		
		System.out.println("Id :" +s.getId());
		System.out.println("Name :" +s.getName());
		s.calcAvg();
		s.findGrade();
		System.out.println("average :" +s.getAvg());
		System.out.println("grade :" +s.getGrade());
		
	}

}
Posted
Updated 20-Aug-20 1:44am
v4
Comments
Sandeep Mewara 20-Aug-20 5:34am    
Your code is incomplete.
Yaswanth Kummar 20-Aug-20 7:02am    
updated my question
Richard MacCutchan 20-Aug-20 7:23am    
What is the problem with this code?

Your code was corrupted while pasting on box and part is missing.
Quote:
Whats the wrong in the code

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Your calcAvg() is based on property marks. You never set it up post taking the input. Thus, the value as zero.

You need to set marks like:
Java
Student s = new Student();
s.setId(id);
s.setName(name);
s.setSub(sub);
s.setMarks(a); //Added - was missing


Now, 2 things:
1. You need to learn about Debugging (assuming eclipse), refer: Debugging the Eclipse IDE for Java Developers | The Eclipse Foundation[^]
2. You need to learn about scopes - local/global, refer: Scope of Variables In Java - GeeksforGeeks[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900