Void method(With return)
public class Display{
public static void main(String[] args){
Display casio = new Display();
casio.add();
}
public void add(){
return;
}
}
Step-by-Step Execution
- Program starts from main()
- Display casio = new Display();
- A new object of class Display is created.
- casio.add();
- The add() method is called.
- Inside add():
- return; simply exits the method
- No value is returned because the method type is void
- Program ends
Output
Mark Calculation Program
public class Marks{
public static void main(String[] args){
int total = sumMarks(100,98,78,95,91);
int avg = average(total);
grade(avg);
}
public static int sumMarks(int m1,int m2,int m3,int m4,int m5){
int total = m1 + m2 + m3 + m4 + m5;
return total;
}
public static int average(int total){
int avg = total/5;
return avg;
}
public static void grade(int avg){
if(avg>=90){
System.out.println("A");
}
else if(avg>=80){
System.out.println("B");
}
else if(avg>=70){
System.out.println("C");
}
else if(avg>=60){
System.out.println("D");
}
else if(avg>=50){
System.out.println("E");
}
else{
System.out.println("Fail");
}
}
}
Step-by-Step Execution
- The program starts from the main() method.
- sumMarks(100,98,78,95,91) method is called.
- Inside sumMarks():
- All marks are added β 462
- The value is returned to main()
- average(total) method is called.
- Inside average():
- Average is calculated β 462 / 5 = 92
- The value is returned to main()
- grade(avg) method is called.
- Inside grade():
- Condition avg >= 90 is checked
- "A" is printed
- The program ends successfully.
Output


Top comments (0)