Assign student to Different Grade and Console interaction

Last week we learned if statement to make a decision. In this week, we have an assignment to have Kelly to decide if a student should be in Grade 1 or 2 or …. , or this student is old enough to graduate from elementary school.

We create a package com.java4kids.tools, then we create a class Multifunction. The method we create is assignToGrade. Please notice that assignToGrade has an assumption int age. Also assignToGrade is a static method

public static void assignToGrade(int age)

It means that it will be called by class directly, not by object. Please see codes below

/**
*
*/
package com.java4kids.tools;

/**
* @author yzhu
*
*/

public class MultiFuction {

/**
* @param age
*/
 public static void assignToGrade(int age){
if(age>=12){
System.out.println(“You are already graduated from Elementary School”);
}else if (age<12&&age>=10){
System.out.println(“You must be in Grade 6”);
}else if (age<10 && age >=9){
System.out.println(“You must be in Grade 5”);
}else if (age<9 && age >=8){
System.out.println(“You must be in Grade 4”);
}else if (age<8 && age >=7){
System.out.println(“You must be in Grade 3”);
}else if (age<7 && age >=6){
System.out.println(“You must be in Grade 2”);
}else if (age<6 && age >=5){
System.out.println(“You must be in Grade 1”);
}else{
System.out.println(“You are normally in kindergarten”);
}

}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MultiFuction.assignToGrade(10);

}
}

multifunction

You can run Multifunction in console and check if the print is “You must be in Grade 6”

Let’s make it more interactive and more fun. We need to read your input from console; therefore, we need some java package to support us doing the console input, and let the system read our input. you may want to know what is package. Please read the reference below.

We create another Class FunctionHandler, in main method, we have to realize two function:

  1. we read the input
  2. we use the input integer to be the age in assignToGrade

By using some API package, we do:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

Then look at my code how can we use the method in these package to help me

package com.java4kids.tools;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class FunctionHandler {

public static void main(String[] args) throws IOException {

// TODO Auto-generated method stub

final String EXIT_COMMAND = “exit”;

InputStreamReader ir = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(ir);

System.out.println(“Enter the student’s age, or ‘” + EXIT_COMMAND + “‘ to quit”);

int age;

while (true) {

System.out.print(“> “);

String input = br.readLine();

if (input.length() == EXIT_COMMAND.length() && input.toLowerCase().equals(EXIT_COMMAND)) {

System.out.println(“Exiting.”);

return;

}else{

System.out.println(“You input your age = “+input);

age = Integer.parseInt(input);

MultiFuction.assignToGrade(age);

}

}

}

functionhandler

Reference:

A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer. You might keep HTML pages in one folder, images in another, and scripts or applications in yet another. Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.

The Java platform provides an enormous class library (a set of packages) suitable for use in your own applications. This library is known as the “Application Programming Interface”, or “API” for short. Its packages represent the tasks most commonly associated with general-purpose programming. For example, a String object contains state and behavior for character strings; a File object allows a programmer to easily create, delete, inspect, compare, or modify a file on the filesystem; a Socket object allows for the creation and use of network sockets; various GUI objects control buttons and checkboxes and anything else related to graphical user interfaces. There are literally thousands of classes to choose from. This allows you, the programmer, to focus on the design of your particular application, rather than the infrastructure required to make it work.

The Java Platform API Specification contains the complete listing for all packages, interfaces, classes, fields, and methods supplied by the Java SE platform. Load the page in your browser and bookmark it. As a programmer, it will become your single most important piece of reference documentation.

Advertisements

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