0% found this document useful (0 votes)
7 views8 pages

SC 2

Uploaded by

MONICA NAHAK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

SC 2

Uploaded by

MONICA NAHAK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Dt : 25/7/2023

faq:

can we declare local variables as "static"?

=>we cannot declare local variables as 'static',because the static variables must

be class-level and cannot be method-level.(Compilation Error)

i
===================================================================

thi
=======

faq:

ipa
can we have Global variables in Java?

=>There is no concept of Global Variables in Java,because the variables


Ma
memories

are within the class or within the Object or within the method.

===================================================================
sh
=====
ate
nk
Ve

*imp

Reading data to Java Programs:


=>To read data to java programs,we use pre-defined methods of "Scanner"
class

from "util" package.(util - Utility)

=>The following are some important pre-defined instance methods from


'Scanner'

class:

i
thi
1.nextByte()

2.nextShort()

ipa
3.nextInt()

4.nextLong()

5.nextFloat()
Ma
6.nextDouble()

7.nextBoolean()
sh
8.nextLine()
ate

=>we use the following syntax to create object for 'Scanner' class:

Scanner s = new Scanner(System.in);


nk

=>"System.in" in Java represent connecting Console input(Keyboard) to


'Scanner'
Ve

class Object.

1.nextByte() : This method is used to read byte-data

Method Signature:

public byte nextByte();


syntax:

byte val = s.nextByte();

2.nextShort() : This method is used to read short-data

Method Signature:

i
public short nextShort();

thi
syntax:

short val = s.nextShort();

ipa
Ma
3.nextInt() : This method is used to read int-data

Method Signature:

public int nextInt();


sh
syntax:
ate

int val = s.nextInt();


nk

4.nextLong() : This method is used to read long-data

Method Signature:
Ve

public long nextLong();

syntax:

long val = s.nextLong();

5.nextFloat() : This method is used to read float-data


Method Signature:

public float nextFloat();

syntax:

float val = s.nextFloat();

i
6.nextDouble() : This method is used to read double-data

thi
Method Signature:

public double nextDouble();

ipa
syntax:

double val = s.nextDouble();


Ma
7.nextBoolean() : This method is used to read boolean-data
sh
Method Signature:
ate

public boolean nextBoolean();

syntax:
nk

boolean val = s.nextBoolean();


Ve

8.nextLine() : This method is used to read String-data

Method Signature:

public String nextLine();

syntax:

String val = s.nextLine();


===================================================================
========

Ex-program : wap to read and display UserDetails?

program : DemoMethods3.java

import java.util.Scanner;

i
thi
class UserDetails

ipa
//Instance Variables memory in Object

String name,mId; Ma
long phNo;
sh
//Instance method

void getUserDetails()
ate

System.out.println("****UserDetails****");
nk

System.out.println("UserName="+name);
Ve

System.out.println("MailId="+mId);

System.out.println("PhoneNo="+phNo);

class DemoMethods3
{

public static void main(String[] args)

Scanner s = new Scanner(System.in);

UserDetails ud = new UserDetails();

i
System.out.println("Enter the UserName:");

thi
ud.name = s.nextLine();

System.out.println("Enter the MailId:");

ipa
ud.mId = s.nextLine();
Ma
System.out.println("Enter the PhoneNo:");

ud.phNo = s.nextLong();

ud.getUserDetails();
sh
ate

}
nk

o/p:

Enter the UserName:


Ve

nit.V

Enter the MailId:

[email protected]

Enter the PhoneNo:

9898981234
****UserDetails****

UserName=nit.V

[email protected]

PhoneNo=9898981234

i
thi
ipa
Ma
sh
ate

=================================================================

Assignment-1:
nk

wap to read and display Product details?


Ve

SubClass : Product

=>code,name,price,qty

=>void getProduct()

MainClass : DemoMethods4.java
Assignment-2:

wap to read and display Book details?

SubClass : BookDetails

=>bCode,bName,bAuthor,bPrice,bQty

i
=>void getBookDetails()

thi
MainClass : DemoMethods5.java

===================================================================

ipa
=====

Ma
sh
ate
nk
Ve

You might also like