Explore Eclipse IDE
a)Ensure you use JAVA perspective
b)Keep 2 tabs open: Package Explorer and Console
To open console,Go to Window>Show View>Console
Understand Dos and Don'ts for EBOX
Rules:
a)Every question should have a class called Main.
public class Main
{
//code here
}
b)Please DONOT create any package.
Revisiting Looping and Decision making constructs
looping constructs
a) for loop
b) while loop
c) do while loop
d) for each loop
decision making constructs
a) if-else
b) switch case (From Java 7 onwads, switch case accepts String as expression)
Practice questions
1. WAP to accept a number from the user and find the numb er of digits in the input
number. If the number entered is negative, print "invalid input", else print the
number.
Sample Input
123
Sample Output
3
Sample Input
-7
Sample Output
invalid input
To accept something from user, lets use
java.util.Scanner class. This class provides various methods like next(),
nextLine(),nextInt(),nextBoolean() to accept String,Integer,Boolean values resp.
What is the difference between next() and nextLine()?
The difference is same as scanf() and gets() of C programming.
Cognizant Technology
When we use nextInt() or in general any non String input method folowed by
nextLine(), the program have chances to throw exception.
Solution:
a)Follow the nextInt() statement by a dummy nextLine().
b) Even for non String input, use nextLine() method plus Wrapper class combination.
To import a package in Eclipse, we press ctrl,shift and o together.
import keyword is same as #include in C Program.
There can be multiple import statements present in a program.
Scanner sc=new Scanner(System.in);
System.in is an instance of java.io.InputStream class.
//int age=sc.nextInt();
int age=Integer.parseInt(sc.nextLine());
String name=sc.nextLine();
System.out.println(age+":"+name);
Integer.parseInt();
Pt1:Method naming convention
method name should start in lower case. If multiple words then only first letter
from 2nd word onwards should be in upper case.
nextInt(),parseInt(),findSum(),computeAverage()
Integer is an example of Wrapper class.
What is a wrapper class?
There are 8 wrapper classes present.
Integer,Boolean,Byte,Short,Long,Float,Double,Character
Wrapper classes provide methods to tranform String input into equivalent pdt.
When a pdt is transformed into ref type,it is known as boxing.
When a ref type is pulled back to pdt,it is known as unboxing.
Integer.toString() converts pdt int to String class reference.
This method is OVERLOADED.
int num=21;
Integer.toString(num);// returns String equivalent of a number of base 10.
Integer.toString(num,base);//return String equivalent of a number (transformed) of
base given as target.
Integer.toString(num,2);//10101
Even the Integer.parseInt() is OVERLOADED.
String s="15";
When we use Integer.parseInt(s);//converts String to pdt int of base 10.
Suppose we write,
Integer.parseInt(s,base);//It returns the decimal (base10) that gets formed from
the String which is present in given base.
Character wrapper class provides methods like isDigit(), isLetter() to find whether
a charater is a digit or not.
WAP to find the sum of digits present in a given string.
Sample Input
ABCDE1234H
Sample Output
10
A=65
a=65+32=97
Z=65+25=90
z=97+25=122
0=48
9=48+9=57
The Date class we are going to use belongs to java.util package.
When we write Date dt=new Date(); it returns current date and time. But, if I need
to work with some other date...
We will use java.text.SimpleDateFormat class.
The above class has two powerfull methods
a) parse that converts String to Date ref
b) format that converts Date ref back to String
Other than that, two other supporting methods are
c) applyPattern(String pattern);
d) setLenient(boolean t);
WAP to accept a date from user as String and print the day.
Sample Input
01-09-2018
Sample Output
Saturday
What is supported?
long x=1234_1234_1597L;