Code 5: Switch case examples
import java.util.Scanner;
public class Test{
public static void main (String args[]){
System.out.println("Program to depict usage of switch case");
Scanner iage = new Scanner(System.in);
System.out.println("Enter age now:");
int age = iage.nextInt();
switch(age) {
case 1:
System.out.println("Kid can crawl");
break;
case 2:
System.out.println("Kid can walk");
break;
case 3:
System.out.println("Kid can talk");
break;
default :
System.out.println("Kid is a trouble! Send him School");
break;
}
}
}
Code 6: While Loop Example
import java.util.Scanner;
public class Test{
public static void main (String args[]){
System.out.println("Teachers Punishment to repeat the same line n times");
Scanner ireps = new Scanner(System.in);
int counter = 0;
System.out.println("Student: how many times should i repeat the statement?");
String statement = "I will learn java successfully.";
int reps = ireps.nextInt();
while (counter < reps){
System.out.println(+counter +". " +statement);
counter++;
}
}
}
Code 7: Multiple Classes Usage
Code a:
public class Test{
public static void main(String args[]){
Class2 Class2obj = new Class2();
Class2obj.setName();
}
}
Code b:
import java.util.Scanner;
public class Class2 {
public void setName(){
Scanner iname = new Scanner(System.in);
System.out.println("Enter you name");
String name = iname.nextLine();
System.out.println("Hello!" +name +"You have created code successfully.");
}
}
Code 8: Using methods with Parameters
Code a:
import java.util.Scanner;
public class Test{
public static void main(String args[]){
Class2 Class2obj = new Class2();
Scanner input = new Scanner(System.in);
System.out.println("Enter your name");
String name = input.nextLine();
Class2obj.setName(name);
}
}
import java.util.Scanner;
public class Test{
public static void main(String args[]){
Class2 Class2obj = new Class2();
Scanner input = new Scanner(System.in);
System.out.println("Enter your name");
String name = input.nextLine();
Class2obj.setName(name);
}
}
Code b:
public class Class2 {
public void setName(String name){
System.out.println("Hello!" +name +" You have created code successfully.");
}
}
No comments:
Post a Comment