Programming In Java | Week 12
Programming In Java | Week 12
Course Link: https://onlinecourses.nptel.ac.in/noc23_cs74/course
Programming Assignment
1. Complete the code to develop an extended version of the ADVANCED CALCULATOR with added special functions that emulates all the functions of the GUI Calculator as shown in the image.
Solution:
outerloop:
for(int i=0; i<seq.length; i++){
if(seq[i]=='C'){ //Clear
operand1=0.0;
operand2=0.0;
output=0.0;
outflag=0;
break outerloop;
}else if(seq[i]=='R'){ //Square Root
for(int j=0; j<i; j++){
o1+=Character.toString(seq[j]);
}
operand1=Double.parseDouble(o1);
output=Math.sqrt(operand1);
outflag=1;
break outerloop;
}
else if(seq[i]=='S'){ //Square
for(int j=0; j<i; j++){
o1+=Character.toString(seq[j]);
}
operand1=Double.parseDouble(o1);
output=Math.pow(operand1,2);
outflag=1;
break outerloop;
}else if(seq[i]=='F'){ //Inverse
for(int j=0; j<i; j++){
o1+=Character.toString(seq[j]);
}
operand1=Double.parseDouble(o1);
output=Math.pow(operand1,-1);
outflag=1;
break outerloop;
}else{
int r=0;
if(seq[i]=='+'||seq[i]=='-'||seq[i]=='/'||seq[i]=='*'||seq[i]=='='){
for(int j=0; j<i; j++){
o1+=Character.toString(seq[j]);
}
operand1=Double.parseDouble(o1);
for(int k=i+1; k<seq.length; k++){
if(seq[k]=='='){
outflag=1;
operand2=Double.parseDouble(o2);
if(seq[i]=='+'){
output=operand1+operand2;
}else if(seq[i]=='-'){
output=operand1-operand2;
}else if(seq[i]=='/'){
output=operand1/operand2;
}else if(seq[i]=='*'){
output=operand1*operand2;
}
break outerloop;
}else{
o2+=Character.toString(seq[k]);
}
}
}
}
}
2. A partial code fragment is given. The URL class object is created in try block.You should write appropriate method( ) to print the protocol name and host name from the given url string.
For example:
https://www.xyz.com:1080/index.htm
protocol://host:port/filename
Solution:
try
{
URL url=new URL("http://www.Nptel.com/java-tutorial");
System.out.println("Protocol: "+url.getProtocol());
System.out.print("Host Name: "+url.getHost());
}
catch(Exception e){System.out.println(e);}
}
}
3. Write a program to create a record by taking inputs using Scanner class as first name as string ,last name as string ,roll number as integer ,subject1 mark as float,subject2 mark as float. Your program should print in the format
“name rollnumber avgmark”.
Solution:
String f = s1.next();
String l = s1.next();
int n = s1.nextInt();
double db = s1.nextDouble();
double db1 = s1.nextDouble();
double avg=(db+db1)/2;
System.out.print(f + l +" "+ n +" "+avg );
4. A program code is given to call the parent class static method and instance method in derive class without creating object of parent class. You should write the appropriate code so that the program print the contents of static method() and instance method () of parent class.
Solution:
public static void main(String[] args)
{
Child c= new Child();
c.testInstanceMethod();
Parent.testClassMethod();
}
}
5. Write a recursive function to print the sum of first n odd integer numbers. The recursive function should have the prototype
” int sum_odd_n(int n) “.
Solution:
return 2*n-1 + sum_odd_n(n-1);