Вы находитесь на странице: 1из 16

Java Programming Notes

Java Programming,

Two Ways:

Coding Running

Window; Dos Prompt


1. Type your Code Compile: javac.filename.java
2. Saving Running: java.filename
3. File Name=program
(class name)
4. File name have extension
(.java)

Coding is writing in notepad


-Structure----
class exe1{
public static void main(String[] args){
statement;
}
}
For running;
1. For window XP: go to run cmd (Enter)
2. For window 2000 or 1998: Use MSDOS Prompt

At MS DOS Prompt or cmd;


Type
cd\folder\bin (enter)
C:\folder\bin>javac exe1.java (press enter for compiling)
C:\folder\bin>java exe1 (press enter for running)

Switching Two Screen (From MS DOS Prompt to notepad or vice versa)


- Press Alt+tab.

Output Statement-

System.out.println(“Hello!”);

Text
Print  continue next line
Println  another line
“\n”  next line also (it should write within semi coma; for example System.out.print(“Hello!\n”);)

Question 1:
Write the following display data.
************
Hello!
************
Coding
class exe1{
public static void main(String[] args){
System.out.println("* * * * * * * * *");
System.out.print("Hello!\n");

System.out.print("* * * * * * * * *");
}
}

Question 2:
Write the following data star triangle.
*
**
***
****
Coding
class exe2{
public static void main(String[] args){
System.out.println("*");

Page - 1 -
Java Programming Notes

System.out.println("* *");
System.out.println("* * *");
System.out.println("* * * *");
}
}

Calculation:
System.out.print(“text” +(formula))

System.out.print(“result ” +(1+2));  display will be { result 3}


System.out.print(“result ” +1+2);  display will be { result 12}

Question 3:
Write the exe3; length is 5 and width is 3 of rectangle. Find the area of rectangle?
Coding
class exe3{
public static void main(String[] args){
System.out.println("Length=5 and width=3");
System.out.print("Area of rectangle is " +(5*3));
}
}

Math function:
Math.pow(base,power);
Math.log(value);
Math.sin(value);
Math sqrt(value);

Question 4:
A square area is 144. Find a side of square?
Coding
class exe4{
public static void main(String[] args){
System.out.println("Area of Square is 144");
System.out.print("A sideo of the square is " +(Math.sqrt(144)));
}
}

Question 5:
Find sin(30)/cos(30) and tan(30)?
Coding
class exe5{
public static void main(String[] args){
System.out.println("[sin(30)/cos(30)] is " +((Math.sin(30))/(Math.cos(30))));
System.out.print("tan(30) is " +(Math.tan(30)));
}
}

Question 6:
Find sin2(45)+cos2(45) ?
Coding
class exe6{
public static void main(String[] args){
System.out.print("square of sin(45)+square of cos(45) is " +
((Math.pow(Math.sin(45),2))+(Math.pow(Math.cos(45),2))));
}
}

Random…!
Math.random()*100 which means random values from 0 to 100 including decimal.
(int)Math.random()*100 which means random values from 0 to 100 including only integer.
System.out.print(“text” +(7/4)) the result will be 1.
System.out.print(“text” +(7/4F)) the result will be 1.75.

Variable..!

Variable

Test Number

String Interger decimal

int Longint Double

Page - 2 -
Java Programming Notes

Integer (int) is from 0 to 32000*104; (Longint) is from 0 to 32000*1032.


Decimal (Double) is upto 1.00*10-324.

Question 7:
Write the program a=4 and b=5.
1. (a+b)*a=?
2. a+b*a=?
3. (a>b)=?
4. (a=≠b)=?
5. (a<b)=?
Code
class exe7{
public static void main(String[] args){
int a=4, b=5;
System.out.println("a=4 and b=5");
System.out.println("(a+b)*a= " +((a+b)*a));
System.out.println("(a+b*a)= " +(a+b*a));
System.out.println("(a>b)= " +(a>b));
System.out.print("(a=b)= " +(a==b) +"\n");
System.out.print("(a<b)= " +(a<b));
}
}

Question 8:
Write a program exe8. double value C°=48. Find conversion of F°? F= (k*C)+32; k=1.8.
Code
class exe8{
public static void main(String[] args){
double k=1.8, C=48;
System.out.print("k=1.8 and C=48" +"\n" +"(F=(k*C)+32)" +"\n");
System.out.print("F= " +((k*C)+32));
}
}

Increasing / Decreasing
++(variable); --(variable):  Pre increase or decrease
(variable)++; (variable)--:  Next increase or decrease

Question 9:
Wirte the program a=4 and b=3. Find data.
a is one more than
a+b=?
b is one more than
a+b=?
a is one less than
a=b=?
b is one less than
a+b=?
Code 
class exe9{
public static void main(String[] args){
int a=4, b=3;
System.out.println("a is one more than " +(++a));
System.out.println("(a+b)= " +(a+b));
System.out.println("b is one more than " +(++b));
System.out.println("(a+b)= " +(a+b));
System.out.println("a is one less than " +(--a));
System.out.println("(a=b)= " +(a==b));
System.out.println("b is one less than " +(--b));
System.out.print("(a+b)= " +(a+b));
}
}

Expression List! (Comparing of Number){<, >, =}


Name=(value1>value2)? Value1:value2;

Question 10:
Write exe10. Given a=4 and b=5. Find maximum value?
Code
class exe10{
public static void main(String[] args){
int a=4, b=5, max;
System.out.println("The values are " +a +"and " +b);
max=(a>b)? a:b;
System.out.print("The maximum value is " +max);
}
}

Page - 3 -
Java Programming Notes

Question 11:
Write exe11. Given a=3 and b=6. Find Minimum value?
Code
class exe11{
public static void main(String[] args){
int a=3, b=6, min;
System.out.println("The values are " +a +" and" +b);
min=(b<a)? b:a;
System.out.print("The minimum value is " +min);
}
}

Question 12:
Write exe12. Given three number a=4, b=5 and c=6. Find maximum value?
Code
class exe12{
public static void main(String[] args){
int a=4, b=5, c=6, max;
System.out.println("The values are " +a +". " +b +" and " +c);
max=(a>b)? a:b;
max=(max>c)? max:c;
System.out.print("The maximum value is " +max);
}
}

Conditional Statement!
If statement—
One line statement---
Format if(condition)statement;

Question 13:
Write a number is 5. This number is defined as whole or negative or zero-level?
Code
class exe13{
public static void main(String[] args){
int a=5;
System.out.println("The given value is " +a);
if(a>0){
System.out.println("The value is whole number.");
}
if(a<0){
System.out.println("The value is negative.");
}
if(a==0){
System.out.println("The value is zero-level.");
}
}
}

Remainder!
Number%divisor=remainder

Question 14:
Write a number is 4, define even or odd?
Code
class exe14{
public static void main(String[] args){
int a=4;
System.out.println("Given value is " +a);
if(a%2==0){
System.out.print("The given value is even");}
if(a%2!=0){
System.out.print("The given value is odd");}
}
}

Question 15:
Write two numbers which are 40 and 60. Find maximum value (use if statement)?
Code
class exe15{
public static void main(String[] args){
int a=40, b=60, max;
System.out.println("The given values are " +a +" and " +b);
if(b>a){
System.out.print("The Maximum Value is " +b);}
if(b<a){
System.out.print("The Maximum Value is " +a);}
}
}

Page - 4 -
Java Programming Notes

Question 16:
The three numbers are 20, 40, and 60. Write a program to find the minimum value (use if statement).
Code
class exe16{
public static void main(String[] args){
int a=20, b=40, c=60, max;
System.out.println("The given values are " +a +", " +b +" and "+c);
max=a;
if(max<b)max=b;
if(max<c)max=c;
System.out.print("The maximum value is " +max);
}
}

Condition!
1. More than one condition
2. One or all condition and
3. Logical
If(condition1 && condition2)  and
If(condition1 || condtion2)  or

Question 17:
A number 45 is given. Check your number whether it is in between 20 to 30 or <=20 (or) >=40. Show the result by means of
“correct” and “incorrect”.
Code
class exe17{
public static void main(String[] args){
int a=45, b=0;
if(a>=20 && a<=30)b=1;
if(b==1){
System.out.print("The value 45 is in between 20 to 30");}
if(b==0){
System.out.print("The value 45 is <=20 (or) >=40");}
}
}

Type Input number in Running Time!


Main(String args[]){
Variable=Integer.parseInt(args[index no.]);

Question 18:
Input your number of miles, then hours for your trip. Find speed.
Code
class exe18{
public static void main(String args[]){
float m, hr;
m=Integer.parseInt(args[0]);
hr=Integer.parseInt(args[1]);
System.out.print("Speed of the trip is " +(m/hr) +" Miles per hours.");
}
}

If Yes/No Statement!


if(condition)
{Yes statement}
else
{No statement}

Question 19:
Input usage meter unit. First 500 units is 10 kyats. Over load meter unit is 15 kyats. Maintenance fee is 100 kyats. Find total
cost?
Code
class exe19{
public static void main(String args[]){
int a, b=0;
a=Integer.parseInt(args[0]);
System.out.println("Usage meter unit is " +a);
if(a<=500){
b=(10*a);}
else{
b=((500*10)+((a-500)*15));}
System.out.print("Total cost is " +(b+100));
}
}

Question 20:
Input two subject marks, define whether your student is pass or fail? This student is pass if his marks exceed 40 marks.
Code
class exe20{
public static void main(String args[]){

Page - 5 -
Java Programming Notes

int a;
String b;
a=Integer.parseInt(args[0]);
System.out.println("The available marks is " +a);
if(a<40){b="FAILED";}
else{b="PASSED";}
System.out.print("The selected student is " +b);
}
}

Block if!
if(condition){
if(condition){
Yes Statement}}
else{
No Statement}

Question 21:
Distinguish the students with a program whether they are failed, passed or passed with distinction?
Code
class exe21{
public static void main(String args[]){
int a;
String b;
a=Integer.parseInt(args[0]);
System.out.println("The available marks is " +a);
if(a>=40){
if(a>=75){
b="PASSED With DISTINCTION!";}
else{
b="PASSED";}}
else{
b="FAILED";}
System.out.print("The selected student is " +b);
}
}

Nested if!
if(condition 1){
Statement 1}
else if(condition 2){
Statement 2}
else if(condition 3){
Statement 3}
else{
no statement}

Question 22:
Input the total marks of the exam. Define the grade?
List mark grade
450 A
360-449 B
240-359 C
240< D
Code
class exe22{
public static void main(String args[]){
int a;
String b;
a=Integer.parseInt(args[0]);
System.out.println("The available marks is " +a);
if(a>=450){b="A";}
else if(a>=360 && a<=449){b="B";}
else if(a>=240 && a<=359){b="C";}
else{b="D";}
System.out.print("The grade is " +b);
}
}

Swtich case!
Switch(condition value){
Case 1: Statement;break;
Case 2: Statement;break;
Default: No statement;break;}

Question 23:
Input sale amount commission rate is-
Amount rate
>10000 10%
10000-5000 5%
<5000-2000 2%
Code

Page - 6 -
Java Programming Notes

class exe23{
public static void main(String args[]){
double a, b=0;
a=Integer.parseInt(args[0]);
System.out.println("Sale amount is " +a);
if(a>10000){
b=((a*10)/100);}
else if(a<=100000 && a>=5000){
b=((a*5)/100);}
else if(a>=2000 && a<5000){
b=((a*2)/100);}
else{
System.out.print("No commission.");}
if(b!=0){
System.out.print("Commission amount is " +b);}
}
}

Question 24:
Input data number list;
1. Sum,
2. Subtract,
3. Product, then calculate the input two numbers.
Code
class exe24{
public static void main(String args[]){
double d=0.0;
int a, b, c;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
c=Integer.parseInt(args[2]);
System.out.println("1. Sum");
System.out.println("2. Subtract");
System.out.println("3. Product");
switch(c){
case 1: d=(a+b);break;
case 2: d=(a-b);break;
case 3: d=(a*b);break;
default: System.out.println("Invalid number");break;}
System.out.print("The result is " +d);
}
}

Question 25:
Select the following item number;
1. Computer, price is $900,
2. CPU, price is $250,
3. Mother Board, price is $250,
4. Printer, price is $150. Show how many quantity is sold and how much revenue is being collected?
Code
class exe25{
public static void main(String args[]){
int a, b, c;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
System.out.println("1. Computer");
System.out.println("2. CPU");
System.out.println("3. Mother Board");
System.out.println("4. Printer");
switch(a){
case 1: c=900; break;
case 2: case 3: c=250; break;
case 4: c=150; break;
default:System.out.print("Invalid Choise");c=0;break;}
if(c!=0){System.out.print("Total revenue is " +(b*c));}
}
}

Question 26:
Input month number. Define the last day of your month number?
Hint 4,6,9,11 are 30 days
2 is 28 or 29 days
1,3,5,7,8,10,12 are 31 days.
Code
class exe26{
public static void main(String args[]){
int a;
String b;
a=Integer.parseInt(args[0]);
switch(a){
case 4: case 6: case 9: case 11: b="30 days.";break;
case 2: b="28 or 29 days.";break;
default: b="31 days.";break;}

Page - 7 -
Java Programming Notes

System.out.print("This month has " +b);


}
}

Frequency (Loop)
1. do….while
2. while
3. for loop

do{
…..statement….}
while(condition value);

Question 27:
To print “hello!” for 10 lines.
Code
class exe27{
public static void main(String[] args){
int a=1;
do{
System.out.println("Hello!");
a++;
}while(a<=10);
}
}

Question 28:
To print only even number from 1 to 20.
Code
class exe28{
public static void main(String[] args){
int a=2;
do{
System.out.println(a);
a=a+2;
}while(a<=20);
}
}

Question 29:
To print prime number list of 1 to 100.
Code
class exe29{
public static void main(String[] args){
int a=1;
System.out.print("2 3 5 7 ");
do{
if(a%2!=0 && a%3!=0 && a%5!=0 && a%7!=0){
System.out.print(a +" ");}
a++;
}while(a<=100);
}
}

Question 30:
Input start number and last number and then list the odd number between them.
Code
class exe30{
public static void main(String args[]){
int a, b, max, min;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
max=(b>a)? b:a;
min=(a<b)? a:b;
a=min; b=max;
do{
if(a%2!=0){
System.out.print(a +" ");}
a++;
}while(a<=b);
}
}

Question 31:
To print a list of number which are divisible by 3 for any input numbers.
Code
class exe31{
public static void main(String args[]){
int a;
a=Integer.parseInt(args[0]);
System.out.println("Input number is " +a);
do{

Page - 8 -
Java Programming Notes

a=a/3;
System.out.println(a +" ");
}while(a>0);
}
}

 While statement!
First value;
while(condition){
statement;}

Question 32:
Input a number to print out prime factor list for the input number. Eg. Input is 45 prime factor lists are 3, 3, 5.
Code
class exe32{
public static void main(String args[]){
int num, a=2, d=1;
num=Integer.parseInt(args[0]);
do{
if(num%a==0){num=num/a; System.out.print(a+" ");}
else{a=a+d; d=2;}
}while(num>1);
System.out.print(".");
}
}

Question 33:
Input a number and then find the count down the input value to reach the smallest; 1.
Code
class exe33{
public static void main(String args[]){
int num;
num=Integer.parseInt(args[0]);
System.out.println("Starting value is " +num);
while(num>0){
System.out.print(num+" "); num--;}
}
}

Question 34:
Input your number of digit value and then convert them to the reverse value.
Code
class exe34{
public static void main(String args[]){
int a, re1, re2;
a=Integer.parseInt(args[0]);
re1=a%10;
a=a/10;
while(a>0){
re2=a%10;
a=a/10;
re1=(re1*10)+re2;}
System.out.print("The reverse value is "+re1);
}
}

For loop!
for(var=start;var<last;var++)
{Statement;}

Question 35:
Print out only even number between 10 and 30.
Code
class exe35{
public static void main(String[] args){
for(int a=10; a<=30; a++)
{if(a%2==0){System.out.print(a+" ");}}
}
}

Question 36:
Print out the values: 1, 2, 3, 4, 5, 4, 3, 2, 1.
Code
class exe36{
public static void main(String[] args){
for(int a=1; a<=9; a++)
{if(a<=5){System.out.print(a+", ");}
else{System.out.print((10-a)+", ");}}
}
}

Page - 9 -
Java Programming Notes

Question 37:
Input a number. Then find factorial value for your input number.
Code
class exe37{
public static void main(String args[]){
int n, b=1;
n=Integer.parseInt(args[0]);
for(int a=1; a<=n; a++)
{b=b*a;}
System.out.print(n+"!= "+b);
}
}

Question 38:
Input a term number n and then find the pi value: pi= 4 – 4/3 + 4/5 – 4/7 + ………..
Code
class exe38{
public static void main(String args[]){
int num, b; double pi=0.0;
num=Integer.parseInt(args[0]);
System.out.println("Numbers of term: "+num);
for(int a=1; a<=num; a++)
{if(a%2==0){b=(-4);}
else {b=4;}
pi=(pi+(b/((2*a)-1)));}
System.out.print("The value of pi is "+pi);
}
}

Question 39:
Input the x value and n terms of exponential.
ex = 1 + x + x2/2! + x3/3! +…..
Code
class exe39{
public static void main(String args[]){
int x, n, c=1; double r=0.00, d;
x=Integer.parseInt(args[0]);
n=Integer.parseInt(args[1]);
System.out.println("x values is "+x);
System.out.println("No. of terms is "+n);
for(int a=1; a<=n; a++)
{c=c*a;
d=Math.pow(x,a);
r=r+(d/c);}
System.out.print("The result is "+(r+1));
}
}

Question 40:
Listing the square of 1 to 9 values.
Code
class exe40{
public static void main(String[] args){
for(int a=1; a<=9; a++)
{System.out.print(Math.pow(a,2)+ " ");}
}
}

Question 41:
To print 0, 1, 2, 1, 2, 3, 2, 3, 4.
Code
class exe41{
public static void main(String[] args){
for(int a=0; a<9; a++)
{System.out.print(((a%3)+(a/3))+ " ");}
}
}

Question 42:
To print fibonancy series 0, 1, 1, 2, 3, 5, 8, 13, …..
Code
class exe41{
public static void main(String[] args){
for(int a=0; a<9; a++)
{System.out.print(((a%3)+(a/3))+ " ");}
}
}

Question 43:
To print numbers from 11 to 50. One line has only 5 numbers.
Code

Page - 10 -
Java Programming Notes

class exe43{
public static void main(String[] args){
for(int a=11; a<=50; a++)
{if(a%5==0){System.out.println(a);}
else {System.out.print(a+ ", ");}}
}
}

Array Value Data


Single array
One row with many columns
Code (declaration)
int[] a={ , , , , };

a[0] a[1] a[2]


int[] a=new int[size];

Question 44:
Array A is {5,3,4,2,5}. Find the array B which is 2*A.
Code
class exe44{
public static void main(String[] args){
int[] A={5,3,4,2,5};
int[] B=new int[5];
System.out.print("The Array A is {5,3,4,2,5}.");
System.out.print("\nThe Array B is ");
for(int a=0; a<5; a++) {
B[a]=(2*A[a]);
System.out.print(B[a]+ ", ");}
}
}

Question 45:
Array A is {-1,3,-2,10,8,9,1}. Find Max and min value?
Code
class exe45{
public static void main(String[] args){
int[] A={-1,3,-2,10,8,9,1}; int min, max;
System.out.print("Array A is {-1,3,-2,10,8,9,1}");
min=max=A[0];
for(int a=1; a<7; a++){
if(max>A[a]){max=A[a];}
if(min<A[a]){min=A[a];}}
System.out.print("\nMax value is "+max);
System.out.print("\nMin value is "+min);
}
}

Question 46:
Input your array number of 4,3,5,6 and next array is 2,3,4,5. Find the multiple of these two arrays.
Code
class exe46{
public static void main(String[] args){
int[] a={4,3,5,6};
int[] b={2,3,4,5};
int[] c=new int[4];
System.out.println("First array is {4,3,5,6}");
System.out.println("Second array is {2,3,4,5}");
System.out.print("Third array is ");
for(int i=0; i<4; i++){
c[i]=a[i]*b[i];System.out.print(c[i]+", ");}
}
}

Question 47:
Your company has 4 class of workers. 1st class has 5 workers and 2nd has 10. The rest 3rd and 4th have 15 and 20 respectively.
Additionally, their salaries are (10, 15, 20, 30) respectively.
1. Find total workers.
2. Find total salaries.
3. Find average salaries.
4. Find the number of workers who get above the average salaries and
5. Find the number of workers who get below the average salaries.
Code
class exe47{
public static void main(String[] args){
int[] n={5,10,15,20};
int[] s={10,15,20,30};
int sum1=0, sum2=0, av, a=0, b=0;
for(int i=0; i<4; i++){
sum1=sum1+n[i];}

Page - 11 -
Java Programming Notes

System.out.print("1. Total number of workers is "+sum1);


for(int i=0; i<4; i++){
sum2=sum2+s[i];}
System.out.print("\n2. Total salaries is "+sum2);
av=sum2/sum1;
System.out.print("\n3. Average salaries is "+av);
for(int i=0; i<4; i++){
if(s[i]>av){a=a+n[i];}
if(s[i]<av){b=b+n[i];}}
System.out.print("\n4. No. of workers who get above av salaries is "+a);
System.out.print("\n5. No. of workers who get below av salaries is "+b);
}
}

Question 48:
Display 10 random values of integer 1 to 20. Then find the max value of amongst them.
Code
class exe48{
public static void main(String[] args){
int[] a=new int[10];
int max=0, r;
for(int i=0; i<=9; i++){
a[i]=(int)(Math.random()*20);
System.out.print(a[i]+" ");}
for(int i=0; i<=9; i++){
if(a[i]>max)max=a[i];}
System.out.print("\nMax value is "+max);
}
}

Question 49:
A array is in the order {3,10,-3,46,7,9,-1}. Rearrange this order in ascending order.
Code
class exe49{
public static void main(String[] args){
int[] a={3,10,-3,46,7,9,-1};
int b;
System.out.print("The original array is ");
for(int i=0; i<=6; i++){
System.out.print(a[i]+" ");}
for(int i=0; i<=5; i++){
for(int j=0; j<=5; j++){
if(a[j]>a[j+1]){b=a[j];
a[j]=a[j+1];a[j+1]=b;}}}
System.out.print("\nThe ascending order array is ");
for(int i=0; i<=6; i++){
System.out.print(a[i]+" ");}
}
}

Multiple array! row


00 01 02 03
10 11 12 13
20 21 22 23
30 31 32 33

column

int[][] var=new int[size][size];

Question 50:
To print 0 1 2 3
1234
2345
4567
Code
class exe50{
public static void main(String[] args){
int[][] a=new int[4][4];
for(int i=0; i<=3; i++){
for(int j=0; j<=3; j++){
a[i][j]=i+j;
if(j==3){System.out.print(a[i][j]+"\n");}
else {System.out.print(a[i][j]+" ");}}}
}
}

Page - 12 -
Java Programming Notes

Question 51:
To print 0 2 2 3
0 4 2 3
0 6 2 3
0 8 2 3
Code
class exe51{
public static void main(String[] args){
int[][] a=new int[4][4];int b=2;
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
if(j==1){a[i][j]=b;b+=2;}
else {a[i][j]=j;}
System.out.print(a[i][j]+" ");}
System.out.println();}
}
}

Question 52:
To print 1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Code
class exe52{
public static void main(String[] args){
int[][] a=new int[4][4];
for(int i=0; i<=3; i++){
for(int j=0; j<=3; j++){
if(i==j){a[i][j]=1;}
else{a[i][j]=0;}
System.out.print(a[i][j]+" ");}
System.out.println();}
}
}

To input a value during running a program.

import java.io.*;

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

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

Question 53:
Calculate a Leap Year.
Code
import java.io.*;
public class exe53{
public static void main(String[] args)throws IOException{
int year=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Year ");
year=Integer.parseInt(br.readLine());
if((year%100==0 && year%400==0) || (year%100!=0 && year%4==0))
{System.out.println("This Year "+year+" is leap year. ");}
else
{System.out.println("This Year "+year+" is not leap year. ");}
}
}

Question 54:
Calculating stock!
Code
import java.io.*;
public class exe54{
public static void main(String[] args)throws IOException{
String code, des, balance, price, m="y";
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter p=new PrintWriter(new FileWriter("Stock.txt"));
while(m.equals("y"))
{
System.out.print("Stock Code ");
code=br.readLine();
System.out.print("Stock Description ");
des=br.readLine();
System.out.print("Stock Balance ");
balance=br.readLine();
System.out.print("Stock Price ");
price=br.readLine();
p.println(code+","+des+","+balance+","+price);
System.out.print("Any more [y/n]?");

Page - 13 -
Java Programming Notes

m=br.readLine();
}
p.close();
}
}

Question 55:
Recalling the Stock.txt.
Code
import java.io.*;
import java.util.*;
public class exe55{
public static void main(String[] args)throws IOException
{
String scd, des, bal, pri, line;
int amount, p,q;
try
{
FileReader fr=new FileReader("Stock.txt");
BufferedReader br=new BufferedReader(fr);
System.out.println("\t\tStock Enquiry.\n ");
line=br.readLine();
while(line!=null)
{
StringTokenizer st=new StringTokenizer(line,",");
scd=st.nextToken();
des=st.nextToken();
bal=st.nextToken();
p=Integer.parseInt(bal);
pri=st.nextToken();
q=Integer.parseInt(pri);
amount=p*q;
System.out.println("Stock Code\t\t"+scd);
System.out.println("Stock Description\t"+des);
System.out.println("Stock Balance\t\t"+bal);
System.out.println("Stock Price\t\t"+pri);
System.out.println("Amount\t\t\t"+amount);
System.out.println();
line=br.readLine();
}
br.close();
}catch(Exception e)
{e.printStackTrace();}
}
}


Byte it could be anything for instance String, int, float, char….. etc.

Question 56:
Functions…!
Code
class exe56{
public static void displaySum1()
{
byte sum=3+7;
System.out.println("Sum of 3 and 7 is "+sum);
}
public static void displaySum2(int num1, int num2)
{
int sum=num1+num2;
String outmsg="sum of "+num1+" and "+num2+" is "+sum;
System.out.print(outmsg);
}
public static int calculateSum(int num1, int num2)
{
return num1+num2;
}
public static void main(String[] args)
{
int a=60, b=120, c;
displaySum1();
displaySum2(a,b);
c=calculateSum(a,b);
System.out.println("\nc = "+c);
}
}

Default function name();


Parameter function name(int/String …..);

Void print Variable type


output at use return
function

Page - 14 -
Java Programming Notes

Question 57:
You class have two functions data of two persons. Input the data of a first person as default data which is “name is Mg Mg” &
“age is 20”. Data of the second person is input from the main which is Aung Aung and 18. Then display the data list from main
class.
Code
class exe57{
public static void person1()
{
System.out.println("\tFirst Person");
System.out.println("Name = Mg Mg.");
System.out.println("Age = 20");
}
public static void person2(String name, int age)
{
System.out.println("\tSecond Person");
System.out.println("Name = "+name);
System.out.println("Age = "+age);
}
public static void main(String[] args)
{
int a=18; String n="Aung Aung";
person1();
person2(n,a);
}
}

Question 58:
//MyInputReader….(adding age);
Code
import java.io.*;
import java.text.*;
public class MyInputReader
{
public static String readText(String msg)
{
String s;
System.out.println(msg);
try
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
s=br.readLine();
}
catch(IOException e)
{ s=" "; }
return s;
}
public static double readNumber(String msg)
{
double num;
DecimalFormat df=new DecimalFormat();
try
{
Number n=df.parse(readText(msg));
num=n.doubleValue();
}
catch(ParseException e)
{num=0;}
return num;
}
public static void main(String[] args)
{
int i=(int)readNumber("How old are you?");
System.out.print("You are "+i+" years old.");
}
}

Question 59:
Evaluating the number whether it is odd or even using the readText function from MyInputReader.
Code
class OddFunction
{
public static boolean odd(int num)
{
return num%2==1;
}
public static void main(String[] args)
{
int i=(int)MyInputReader.readNumber("Enter Number, ");
if(odd(i))System.out.println("It is ODD.");
else System.out.println("It is EVEN.");
}
}

Page - 15 -
Java Programming Notes

Field method
Field method  1. Private
2. Public
3. Protected

Private  running within its own class


Public  accessible by all class
Protected  Class by class not main

Page - 16 -

Вам также может понравиться