Skip to content
August 30, 2008 / Narayan

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 .

Advertisements

2 Comments

Leave a Comment
  1. EJP / Jun 9 2009 4:11 am

    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.

    • Narayan / Jun 9 2009 11:22 am

      well dat’s need to be elaborate……….
      man!
      you have reminded now it’s my job to do this

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: