Saturday, 3 July 2021

Naming Convention/Scanner Class || Introduction of Java (Part-4)

• Naming Conventions

• System.Out 

• System.in 

• Scanner Class

PROGRAM


Naming Conventions

class name, interface name – First Letter of each word must be in upper case and rest in lower case. – e.g. String, StringBuffer, FileInputStream, ActionListener etc.

 • method name, variable name – First word in lower case – For rest of words First Letter of each word must be in upper case and rest in lower case 

– e.g. main(), print(), drawLine(), drawRoundRect() , toUpperCase( ) etc. 

package name – should be in lowercase letter 

– e.g. java, lang, sql, util etc. 

constants name – should be in uppercase letter. – Words separated by underscore

 – e.g. RED, YELLOW, MAX_PRIORITY etc.


Program Input

Input to our java program can be provided by using different ways. 

Console Input (using System.in)

 – GUI Interface

 – Command Line Arguments

 – Input Dialog Box 




• System.out Object 

 • This object is attached to our standard output device.

 • This object is used to send data to standard output device. 

• It provides us methods print(), println(), printf()..

   etc to write data into the object .


• System.in Object 

• This object is attached to our standard input device. 

• This object is used to read data from standard input device.0 

• It provides us method read() to read raw/binary data from the object 


System.in

We can not directly easily use this System.in Object because it returns byte type of data.
 i.e. raw data

We have to wrap it in different objects to read its data such as 

– Scanner

 – BufferedReader

 – etc


Class Scanner – java.util


It is used to scan data from System.in Object. 

Scanner Object can be created by using syntax:-

 Scanner stdin=new Scanner(System.in);

methods:- 

String next() – it returns the next token/word from the scanner. 

String nextLine() – it returns the next line as a string.  

int nextInt() – it scans the next token as an int value.  

long nextLong() – it scans the next token as a long value. .

float nextFloat() – it scans the next token as a float value. 

double nextDouble() – it scans the next token as a double value.

  

Program's


  • WAP to read radius of circle and find its area and circumference.

package com.company;
import java.util.Scanner;
public class Easy
{
public static void main(String[] args)
{

Scanner likein=new Scanner(System.in);
System.out.print("Enter Radius ");
int r=likein.nextInt();
double a=3.14*r*r;
double c=2*3.14*r;
System.out.println("Area is "+a);
System.out.println("Circumference is "+c);
}
}

Output:- 

Enter Radius 
 6 
Area is 113.03999999999999
Circumference is 37.68


  • WAP to read length and breadth of rectangle and find its area and perimeter.

package com.company;
import java.util.Scanner;
public class Easy
{
public static void main(String[] args) {

Scanner likein =
new Scanner(System.in);
System.
out.print("Enter Length and Breadth:");
int l = likein.nextInt();
int b = likein.nextInt();

int a = l * b;
int p = 2 * (l + b);

System.
out.println("Area is " + a);
System.
out.println("Perimeter is " + p);
}
}

Output:- 

Enter Length and Breadth:
10 5
Area is 50
Perimeter is 30

1 comment:

What is ladder if || Java Ladder if statement.

 Ladder if statement If a if statement is used within an else statement then such a control structure is called as ladder if statement. Synt...