Using Switch

public class Grading {
	//attribute
	private int marks;
	String division;

	public Grading(int marks) {
		//inputing valueof marks
		this.setMarks(marks);

	}

	//accessor methods
	public void setMarks(int sm){
		this.marks = sm;
	}
	public int getMarks(){
		return marks;
	}
	public void setDiv(String div){
		this.division = div;
	}
	public String getDiv(){
		return this.division;
	}

	//now initializing the division
	public void test(){
	//looping for using switch
	for (int x = 1; x<=5; x++){
          //switch method
	switch(x){
		case 1:
                //first condition for distinction
		if(this.marks >= 80 && this.marks <100)
	        this.setDiv("Distinction");
		break;

		case 2:
		if(this.getMarks() <80 && this.getMarks()>=60)
		this.setDiv("First Division");
		break;
	        case 3:
		if(this.getMarks() <60 && this.getMarks()>=50)
		this.setDiv("Second Division");
		break;
		case 4:
		if(this.getMarks() <50 && this.getMarks()>=40)
		this.setDiv("Third Division");
		break;
		case 5:
                if(this.getMarks()<40)this.setDiv("FAIL!");break;

		}
	    }
	}

	public void display(){
		//FOR DISPLAYING
	System.out.println("You have obtain "
           +this.getMarks()+" Marks");
	System.out.println("You stood at " + this.getDiv());
	}
	public static void main (String[] args) {
		//you can change your marks here (80);
		Grading g = new Grading(80);
		g.test();
		g.display();
	}
}

In this code you can see the use of switch method .

2 Responses

  1. This example demonstrates nothing. It is completely pointless. If you remove the ‘for’ loop and the switch statements and case labels and breaks the code will perform identically.

Leave a Reply