0% found this document useful (0 votes)
33 views11 pages

Oop Lab Work

Uploaded by

arman64658852
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)
33 views11 pages

Oop Lab Work

Uploaded by

arman64658852
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/ 11

Lab Task

Submitted by:
Arslan Ahmed Khan
FA23-BCS-001
Submitted to
Umiar Mujtaba
Program 1:
package bookseller;

import java.util.Scanner;

/**
*
* @author fa23-bcs-001
*/
public class BookSeller {
public int a;
public int setter(){
System.out.println("Enter number of books purchased this month");
Scanner input=new Scanner(System.in);
a=input.nextInt();
return a;
}
public BookSeller(){
a=0;
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
BookSeller b=new BookSeller();
int c=b.setter();
int points;
if(c==0){
points=0;
System.out.println("Points="+points);}
else if(c==1){
points=5;
System.out.println("Points="+points);}
else if(c==2){
points=15;
System.out.println("Points="+points);}
else if(c==3){
points=30;
System.out.println("Points="+points);}
else if(c>=4){
points=60;
System.out.println("Points="+points);}
}

Program 2
package maxndex;

import java.util.Scanner;
/**
*
* @author fa23-bcs-001
*/
public class Maxndex {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

int firstNumber = scanner.nextInt();


if (firstNumber == 0) {
System.out.println(0);
return;
}

int maxValue = firstNumber;


int maxIndex = 1;
int currentIndex = 2;
while (true) {
int number = scanner.nextInt();
if (number == 0) {
break;
}
if (number > maxValue) {
maxValue = number;
maxIndex = currentIndex;
}
currentIndex++;
}
System.out.println(maxIndex);

scanner.close();
}
}
Program 3
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string: ");
String s = scanner.nextLine();
int totalLettersAndSpaces = countLettersAndSpaces(s);
System.out.println("Total letters and spaces: " + totalLettersAndSpaces);
}

public static int countLettersAndSpaces(String s) {


int count = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isLetter(c) || c == ' ') {
count++;
}
}
return count;
}
}
Program 4
public class Main {
public static void main(String[] args) {

String lowerCase = "word";


String upperCase = capitalize(lowerCase);
System.out.println(upperCase);

static String capitalize(String lowerCase) {


return lowerCase.substring(0,1).toUpperCase() + lowerCase.substring(1);
}
}

Program 5
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a positive real number (a): ");
double a = scanner.nextDouble();
System.out.println("Enter a non-negative integer (n): ");
int n = scanner.nextInt();

double result = power(a, n);

System.out.println(a + " raised to the power of " + n + " is: " + result);
}

public static double power(double a, int n) {


if (n == 0) {
return 1;
}
return a * power(a, n - 1);
}
}
Program 6:
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int[] ratings = new int[40];

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


for (int i = 0; i < ratings.length; i++) {
System.out.print("Enter rating ");
ratings[i] = scanner.nextInt();
}

int sum = 0;
int highest = ratings[0];
int lowest = ratings[0];

for (int i = 0; i < ratings.length; i++) {


int rating = ratings[i];
sum += rating;
if (rating > highest) {
highest = rating;
}
if (rating < lowest) {
lowest = rating;
}
}

double average = (double) sum / ratings.length;

System.out.println("Summary of the poll:");


System.out.println("Total number of ratings: " + ratings.length);
System.out.println("Average rating: " + average);
System.out.println("Highest rating: " + highest);
System.out.println("Lowest rating: " + lowest);
}
}
Program 7:
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the size of the array: ");


int size = scanner.nextInt();

int[] originalArray = new int[size];


int[] reversedArray = new int[size];

System.out.println("Enter the elements of the array:");


for (int i = 0; i < size; i++) {
originalArray[i] = scanner.nextInt();
}

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


reversedArray[i] = originalArray[size - 1 - i];
}

System.out.println("Reversed array:");
for (int i = 0; i < size; i++) {
System.out.print(reversedArray[i] + " ");
}
}
}

Program 8:
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int[][] array = new int[3][4];

System.out.println("Enter the elements of the 3x4 array:");

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


for (int j = 0; j < 4; j++) {
System.out.print("Enter element at [" + i + "][" + j + "]: ");
array[i][j] = scanner.nextInt();
}
}

int maxRowSum = Integer.MIN_VALUE;


int maxRowIndex = -1;

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


int rowSum = 0;
for (int j = 0; j < 4; j++) {
rowSum += array[i][j];
}
if (rowSum > maxRowSum) {
maxRowSum = rowSum;
maxRowIndex = i;
}
}

int maxColSum = Integer.MIN_VALUE;


int maxColIndex = -1;

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


int colSum = 0;
for (int i = 0; i < 3; i++) {
colSum += array[i][j];
}
if (colSum > maxColSum) {
maxColSum = colSum;
maxColIndex = j;
}
}

System.out.println("Row with maximum sum: Row " + (maxRowIndex + 1) + " with sum " +
maxRowSum);
System.out.println("Column with maximum sum: Column " + (maxColIndex + 1) + " with sum " +
maxColSum);
}
}
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int[][] array = new int[3][4];

System.out.println("Enter the elements of the 3x4 array:");

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


for (int j = 0; j < 4; j++) {
System.out.print("Enter element at [" + i + "][" + j + "]: ");
array[i][j] = scanner.nextInt();
}
}

int maxRowSum = Integer.MIN_VALUE;


int maxRowIndex = -1;

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


int rowSum = 0;
for (int j = 0; j < 4; j++) {
rowSum += array[i][j];
}
if (rowSum > maxRowSum) {
maxRowSum = rowSum;
maxRowIndex = i;
}
}

int maxColSum = Integer.MIN_VALUE;


int maxColIndex = -1;

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


int colSum = 0;
for (int i = 0; i < 3; i++) {
colSum += array[i][j];
}
if (colSum > maxColSum) {
maxColSum = colSum;
maxColIndex = j;
}
}

System.out.println("Row with maximum sum: Row " + (maxRowIndex + 1) + " with sum " +
maxRowSum);
System.out.println("Column with maximum sum: Column " + (maxColIndex + 1) + " with sum " +
maxColSum);
}
}

You might also like