Professional Documents
Culture Documents
Lab Task 1
SUBMITTED TO:
SUBMITTEED BY:
CLASS:
BCS-3B
1. Write a function that takes two numbers as argument and finds the value of one
number raised to the power of another.
SOLUTION CODE:
import java.util.*;
int power = 1;
for (int i = 0; i<exponent;i++){
power= power*base;
}
return power;
}
}
2. Write a function that reads in 5 numbers; stores them in an array and calculates
and the number of odd and even numbers. If user enters 0, it should not be counted.
SOLUTION CODE:
import java.util.*;
array[0]=array[number1];
array[1]=array[number2];
array[2]=array[number3];
array[3]=array[number4];
array[4]=array[number5];
//method calling
evenodd(array);
int countodd=0;
int counteven=0;
int countzero=0;
for(int i = 0;i<=4;i++){
if(array1[i]%2 == 0){
counteven++;
}
else if(array1[i]==0){
countzero++;
}
else{
countodd++;
}
}
System.out.println("ODD ELEMNTS ARE "+countodd);
System.out.println("ODD ELEMNTS ARE "+counteven);
}
}
SOLUTION FILE:
import java.util.*;
System.out.println(concate(s1,s2));
}
static String concate(String a , String b){
String s3= a+b;
return s3;
}
}
4. Write a function that sums the value of all elements in an array. Display the
Average also.
SOLUTION FILE:
import java.util.*;
//declaration of an array
int [] array =new int[6];
for(int i = 0;i<6;i++){
array[i]=input.nextInt();
}
System.out.print("SUM OF ELEMENTS ARE ");
System.out.println(sum(array));
int sum1 = 0;
for(int i = 0;i<6;i++){
sum1=sum1+array1[i];
}
return sum1;
for(int i = 0;i<6;i++){
sum = sum+array[i];
}
int average = sum/6;
return average;
}
}
5. Write two functions to find the lowest and highest value from an array. Array
should be filled by user.
SOLUTION FILE:
import java.util.*;
//declaration of an array
int [] array =new int[6];
for(int i = 0;i<6;i++){
array[i]=input.nextInt();
}
System.out.println(maxvalue(array));
System.out.print("minimum value in array is ");
System.out.println(minvalue(array));
}
int max = 0;
for(int i=0;i<6;i++){
if(array1[i]>0){
max=array1[i];
}
}
return max;
for(int i=0;i<6;i++){
if(array1[i]<min){
min=array1[i];
}
}
return min;
}
}
6. Write a function that Asks the user to enter the assessment of their telephone
service.
He should enter 1 for excellent Service
He should enter 2 for Good Service
He should enter 3 for Average Service
He should enter 4 for Below Average Service
He should enter 5 for poor Service
Take Input from 50 users and store in a 1 D array. Now calculate and display how
many users rated the system Excellent, Good, Average, Below Average and Poor.
SOLUTION CODE:
import java.util.*;
for(int i= 1;i<50;i++){
array[i]= number;
}
assesmentoftelephone(array);
int countexcellent = 0;
int countgood = 0;
int countaverage = 0;
int countbelowavg = 0;
int countpoor = 0;
int countelse=0;
for(int i = 1;i<50;i++){
if(array1[i]==1){
countexcellent++;
}else if(array1[i]==2){
countgood++;
}else if(array1[i]==3){
countaverage++;
}else if(array1[i]==4){
countbelowavg++;
}else if(array1[i]==5){
countpoor++;
}else{
countelse++;
7. The Internet and the web are enabling more people to interconnect, join a
cause, voice opinions, and so on. In this program, you’ll write a simple polling program
that allows users to rate five social-consciousness issues from 1 (least important) to 10
(most important).
Pick five causes that are important to you (e.g., political issues, global environmental
issues). Use a one-dimensional array topics (of type String) to store the five causes.
To summarize the survey responses, use a 5-row, 10-column two-dimensional array
responses (of type int), each row corresponding to an element in the topics array and
each column represents the responses given by a person.
(Responses will be taken from ten persons)
When the program runs, it should ask the user to rate each issue. Store the responses in
the two-dimensional array. Now the program should display a summary of the results,
including:
SOLUTION FILE:
import java.util.*;
double high = 0;
int ind = 0;
if (high<arrav[i]){
high=arrav[i];
ind = i;
}
double low = 999;
int indexl = 0;
if (low>arrav[i]){
low=arrav[i];
ind = i;
}
}
SOLUTION FILE:
import java.util.*;
System.out.println(replacement(oldString));
9. Write a function that asks the user to enter an email address. Extract the
username from the email address and return it to main.
e-g if user enters student@gmail.com , the function should return “student”.
SOLUTION FILE:
import java.util.*;
System.out.println(extraction(username));
String strt="";
for(int i = 0;i<str.length();i++){
f((str.charAt(i)>='a' && str.charAt(i)<='z')||
(str.charAt(i)>='A'&&str.charAt(i)<='z')||
(str.charAt(i)>='0'&&str.charAt(i)<='9')){
strt = strt + str.charAt(i);
}
else if(str.charAt(i)=='@'){
break;
}
else{
}
return strt;
}
}