You are on page 1of 18

OOP (java)

 
Lab Task 1
 
 
SUBMITTED TO:   

Mam Sneeha Amir

SUBMITTEED BY: 

Shayan Zameer SP21-BCS-088

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.*;

public class Question1{

public static void main(String []args){


Scanner input = new Scanner(System.in);

System.out.println("ENTER FIRST NUMBER");


int firstnum= input .nextInt();
System.out.println("ENTER SECOND NUMBER");
int secondnum= input .nextInt();

System.out.print("power of a number is ");


System.out.println(power(firstnum,secondnum));

static int power(int base, int exponent ){

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.*;

public class Question2{

public static void main(String []args){

Scanner input = new Scanner(System.in);


System.out.println("ENTER NUMBER 1");
int number1=input.nextInt();
System.out.println("ENTER NUMBER 2");
int number2=input.nextInt();
System.out.println("ENTER NUMBER 3");
int number3=input.nextInt();
System.out.println("ENTER NUMBER 4");
int number4=input.nextInt();
System.out.println("ENTER NUMBER 5");
int number5=input.nextInt();
//declaration of an array
int [] array =new int[6];

array[0]=array[number1];
array[1]=array[number2];
array[2]=array[number3];
array[3]=array[number4];
array[4]=array[number5];

//method calling
evenodd(array);

static void evenodd(int []array1){

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);

}
}

3. Write a function that concatenates two strings.

SOLUTION FILE:
import java.util.*;

public class Question3{


public static void main(String []args){

Scanner input = new Scanner(System.in);

System.out.println("Enter First String");


String s1 = input.next();

System.out.println("Enter Second String");


String s2 = input.next();

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.*;

public class Question4{

public static void main(String []args){


Scanner input = new Scanner(System.in);

//declaration of an array
int [] array =new int[6];

System.out.println("Enter element in array");

for(int i = 0;i<6;i++){
array[i]=input.nextInt();
}
System.out.print("SUM OF ELEMENTS ARE ");
System.out.println(sum(array));

System.out.print("AVERAGE OF ELEMENTS ARE ");


System.out.println(average(array));
}

public static int sum(int [] array1){

int sum1 = 0;
for(int i = 0;i<6;i++){

sum1=sum1+array1[i];
}
return sum1;

public static int average(int []array){


int sum = 0;

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.*;

public class Question5{

public static void main(String []args){


Scanner input = new Scanner(System.in);

//declaration of an array
int [] array =new int[6];

System.out.println("Enter element in array");

for(int i = 0;i<6;i++){
array[i]=input.nextInt();
}

System.out.print("maximum value in array is ");

System.out.println(maxvalue(array));
System.out.print("minimum value in array is ");
System.out.println(minvalue(array));
}

static int maxvalue(int[]array1){

int max = 0;

for(int i=0;i<6;i++){
if(array1[i]>0){
max=array1[i];
}

}
return max;

static int minvalue(int [] array1){

int min = 999999;

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.*;

public class Question6{

public static void main(String []args){

Scanner input = new Scanner(System.in);

int []array = new int[50];

for(int i= 1;i<50;i++){

int number = input.nextInt();

array[i]= number;

}
assesmentoftelephone(array);

static void assesmentoftelephone(int[]array1){

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++;

System.out.println("excellent sevice are" +countexcellent);

System.out.println("good sevice are" +countgood);

System.out.println("average sevice are" +countaverage);

System.out.println("below average sevice are"


+countbelowavg);

System.out.println("poor sevice are" +countpoor);

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:

a) Show the average of the ratings for each issue.


c) Which issue received the highest point total? Display both the issue and the
point total.
d) Which issue received the lowest point total? Display both the issue and the
point total.

SOLUTION FILE:

import java.util.*;

public class Question7{

public static void main(String[] args){


Scanner input = new Scanner(System.in);

String[] cause = {"Curroption","Space Waste","Global


Warming","Poverty","Deforestation"};
int[][] rating = new int[5][10];

for(int i = 0 ; i<=rating[0].length-1; i++){


for(int j = 0 ; j<=rating.length-1; j++){
int user = i+1;
System.out.println("User "+user+" enter the rating for
"+cause[j] );
rating[j][i] = input.nextInt();
}
}
int average1 = 0;
int average2 = 0;
int average3 = 0;
int average4 = 0;
int average5 = 0;
for(int i = 0 ; i<=4 ; i++){
int sum = 0;
if(i==0){
for(int j = 0; j<=rating[0].length-1; j++){
sum = sum+rating[i][j];
average1 = sum;
}
}
else if(i==1){
for(int j = 0; j<=rating[0].length-1; j++){
sum = sum+rating[i][j];
average2 = sum;
}
}
else if(i==2){
for(int j = 0; j<=rating[0].length-1; j++){
sum = sum+rating[i][j];
average3 = sum;
}
}
else if(i==3){
for(int j = 0; j<=rating[0].length-1; j++){
sum = sum+rating[i][j];
average4 = sum;
}
}
else{
for(int j = 0; j<=rating[0].length-1; j++){
sum = sum+rating[i][j];
average5 = sum;
}
}
}
double aver1 = average1/10;
double aver2 = average2/10;
double aver3 = average3/10;
double aver4 = average4/10;
double aver5 = average5/10;

double[] arrav = {aver1,aver2,aver3,aver4,aver5};

double high = 0;

int ind = 0;

for (int i = 0 ; i<=arrav.length-2 ; i++){

if (high<arrav[i]){
high=arrav[i];
ind = i;

}
double low = 999;

int indexl = 0;

for (int i = 0 ; i<=arrav.length-2 ; i++){

if (low>arrav[i]){
low=arrav[i];
ind = i;

for(int i = 0 ; i<=4 ; i++){


System.out.println("Average rating for "+cause[i]+" is "+arrav[i]);
}

System.out.println("The issue with the most ratings is "+ cause[ind]


+ " with an average of "+high);

System.out.println("The issue with the least ratings is "+ cause[ind]


+" with an average of "+low);

}
}

8. Write a function that replaces T with O in the String given below:


WelcTme tT TutTrialspTint.cTm
Return the modified string to main.

SOLUTION FILE:
import java.util.*;

public class Question8{

public static void main(String []args){


Scanner input = new Scanner(System.in);

String oldString = "WelcTme tT TutTrialspTint.cTm";

System.out.println(replacement(oldString));

static String replacement(String s2){

String newstring = s2.replace('T','O');


return newstring;
}

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.*;

public class Question9{

public static void main(String []args){


Scanner input = new Scanner(System.in);

System.out.println("ENTER USER NAME");


String username = input.next();

System.out.println(extraction(username));

static String extraction(String str){

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{

System.out.println("Enter valid username");


break;
}

}
return strt;

}
}

You might also like