Untitled

mail@pastecode.io avatar
unknown
java
21 days ago
26 kB
50
Indexable
Never
EX 1(a) :
Imagine you're a software developer working on a project in a remote village. One day, an
elderly villager approaches you, intrigued by your work. They share stories of their youth,
when converting temperatures required complex calculations. Curious, they ask how your
program, which converts Celsius to Fahrenheit, might have impacted their past.


Coding:
import java.util.Scanner;
public class CelsiusToFahrenheit {
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

double celsius = scanner.nextDouble();

double fahrenheit = (celsius * 9/5) + 32;
/
System.out.println("Temperature in Fahrenheit: " + fahrenheit);

scanner.close();
}
}

...........................................................................................
EX 1(b) :
You are developing a BMI calculator for a health app. During the onboarding process, users
are prompted to input their weight and height. Implement a program where users can enter
their weight in kilograms and height in meters. Your program should then compute and
display their Body Mass Index (BMI). This tool helps users track their health and fitness
progress.



Coding:
import java.util.Scanner;
public class BMICalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

double weight = scanner.nextDouble();

double height = scanner.nextDouble();

double bmi = weight / (height * height);
System.out.println("Your BMI is: " + (int)bmi);
scanner.close();
}
...........................................................................


EX 1(c) :
A group of friends explores an ancient code-breaking game. Each player inputs a single
character, unveiling hidden messages encoded in ASCII. As they decipher symbols, they
uncover clues to an ancient treasure. Help them by Implement a code to find a ascii value of
a character


Coding:
import java.util.Scanner;
public class CharacterToASCII
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a character: ");
char character = scanner.next().charAt(0);
int asciiValue = (int) character;
System.out.println("Character: " + character);
System.out.println("ASCII value: " + asciiValue);
scanner.close();
}


.........................................................................................
EX 1(d) :
You're tasked with developing a Java program for a binary-to-decimal converter tool. The
program expects users to input a binary number as a string. After receiving the input, it
converts the binary number to its decimal equivalent and prints the decimal value as an
integer

coding:


import java.util.*;
public class BinarytoDecimal{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String binarystring = sc.nextLine();
        int decimalValue = Integer.parseInt(binarystring,2);
        System.out.println("Binary to Decimal:"+decimalValue);
    }
}

........................................................................................
EX 1(e) :
You're tasked with developing a simple currency converter tool. The program should prompt
users to input an amount in USD and convert it to EUR using a predefined exchange rate.
Upon input, it calculates and displays the converted amount in EUR. This tool facilitates
quick currency conversions, aiding users in international financial transactions and budget
planning.


import java.util.*;
public class UtoE{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        double a = 0.92;
        double b=sc.nextDouble();
        double c=b*a;
        System.out.println(" "+(int)c);
    }
}

............................................................................................
EX 2(a) :
You're developing a retail billing system. Develop a program that calculates the total cost of
a purchase after applying discounts. If the purchase is over $1000, apply a 10% discount;
between $500 and $1000, apply 5%; otherwise, no discount. Prompt users for the purchase
amount, then display the total cost after discount.


import java.util.*;
public class discount{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        double a =sc.nextDouble();
        double b;
        if(a>1000){
            b=(a*0.9);
        }
        else if(a>=500 && a<=1000){
            b=(a*0.95);
        }
        else{
            b=a;
        }
        System.out.println("Discount Amount:"+b);
    }
}


....................................................................................

EX 2(b) :
You've created a simple program that prompts users to input a number. Once the number is
entered, the program analyzes it to determine if it's positive, negative, or zero. Based on this
analysis, it provides immediate feedback to the user regarding the nature of the number.

coding :



import java.util.*;
public class PNZ{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        double number=sc.nextDouble();
        if(number>0){
            System.out.println("The entering number is positive.");
            
        }
        else if(number<0){
            System.out.println("the number is negative");
        }
        else{
            System.out.println("the number is zero");
        }
    }
}

...................................................................................

EX 2(c) :
You're building a geometry tool for analyzing triangles. Design a program that prompts users
to input the lengths of three sides of a triangle, determines if it's equilateral, isosceles, or
scalene using if statements, and displays the result.


coding:

import java.util.*;
public class angle{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        double side1=sc.nextDouble();
        double side2=sc.nextDouble();
        double side3=sc.nextDouble();
        
        if(side1==side2&&side2==side3){
            System.out.println("Triangle");
        }
        else if(side1==side2||side1==side3||side2==side3){
            System.out.println("Isosoluse");
        }
        else{
            System.out.println("Scelne");
        }
    }
}

.................................................................................

EX 2(d) :
You're developing a calendar utility to translate numerical representations of weekdays into
their respective names. Design a program prompting users to input a number representing a
day (1 for Monday, 2 for Tuesday, …, 7 for Sunday), and output the corresponding day name
using a switch statement.

coding:

import java.util.*;
public class Day{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int dayNumber=sc.nextInt();
        String dayName;
        switch(dayNumber)
        {
        case 1:
            dayName="Monday";
            break;
            
            case 2:
                dayName="Tuesday";
                break;
                case 3:
                dayName="Wednesday";
                break;
                case 4:
                dayName="Thursday";
                break;
                case 5:
                dayName="Friday";
                break;
                case 6:
                    dayName="Saturday";
                break;
                case 7:
                    dayName="Sunday";
                break;
                default:
                dayName="invalid";
                break;
        }
                System.out.println("The day is:"+dayName);
    }






    
}

....................................................................


EX 3(a) :
Imagine you're a teacher creating a fun learning tool. With your program, generate
multiplication tables from 1 to 10 for each number entered. Every loop iteration reveals a
new set of answers, fostering a deeper understanding of multiplication.



import java.util.*;
public class tables{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int number = sc.nextInt();
        System.out.println("Multiplication table for 5:");

        for(int i=1;i<=10;i++){
            System.out.println(number+"×"+i+"="+(number*i));
        }
    }
}
................................................................................

EX 3(b) :
Imagine you're a mathematical consultant assisting clients with number inquiries. A user
enters a positive integer, seeking its factors. Implement a program to help the user to find the
factors the numbers.

coding:

import java.util.Scanner;
public class FactorFinder {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
if (number <= 0) {
System.out.println("Error: Please enter a positive integer.");
return;
}
int divisor = 1;
System.out.print("Factors of " + number + ": ");
while (divisor <= number) {
if (number % divisor == 0) {
System.out.print(divisor + " ");
}
divisor++;
}
scanner.close();
}
}

------------------------------------------------------------------------------------------------------------------------


EX 3(c) :
In a school competition, students are participating in a relay race. The race organizer needs
to arrange batons for each team. They have batons in sets, where each set contains a
certain number of batons. However, they need to find out the minimum number of sets
required to distribute an equal number of batons to each team. Implement a program to help
the race organizer find the minimum number of sets required to distribute batons equally
among all teams. Assume that each team requires the same number of batons, and the
number of batons in each set and the number of teams are provided as inputs


coding: 


import java.util.*;
class Event{
    public static void main(String args[]){
        Scanner in=new Scanner(System.in);
        int a=in.nextInt();
        int b=in.nextInt();
        int i=1;
        while(true){
            if((a*i)%b==0){
                System.out.println(a*i);
                break;
            }
            i++;
        }
    }
}
------------------------------------------------------------------------------------------------------
EX 3(d) :
You're tasked with developing a tool that computes squares of numbers within specified
limits. Imagine you're embarking on a journey through the realm of numbers, exploring their
squares. Implement a program using a do-while loop to iterate through these numbers,
uncovering their squares along the way.

coding:

import java.util.*;
class square{
    public static void main(String args[]){
        Scanner kr=new Scanner(System.in);
        int a=kr.nextInt();
        int i=1;
        System.out.print("Squares of numbers from 1 to "+a+":");
        do{
            int b=i*i;
            System.out.print(" "+b);
            i++;
        }
        while(i<=a);
    }
}
---------------------------------------------------------------------------------------------------------------------
EX 4(a) :
Imagine you're a curious explorer delving into the world of prime numbers. Envision seekers
inputting their chosen numbers eagerly. Implement a program to traverse through each
number's factors up to its square root, determining its primality. Each iteration brings you
closer to unraveling the mysteries of prime numbers.


coding:


import java.util.*;
class prime{
        public static void main(String args[]){
            Scanner in=new Scanner(System.in);
            int num=in.nextInt();
            int b=0;
            if(num==0||num==1){
                System.out.println(num+" is not a prime number.");
                
            }
            else{
                int num2=num/2;
                for(int i=2;i<=num2;i++){
                    if(num%i==0){
                 System.out.println(num+" is not a prime number.");
                        b=1;
                        break;
                    }
                }
                if(b==0){
                    System.out.println(num+" is a prime number.");
                }
            }
        }
}
---------------------------------------------------------------------------------------------------------------------------------
EX 4(b) :
Imagine you're a software developer creating a tool for numerical transformations. A user,
fascinated by number manipulation, inputs a positive integer. Implement a program to
gracefully reverse the digits of their number using a while loop, providing them with the
mirror image of their input for exploration

coding:

import java.util.*;
class Rev{
    public static void main(String arg[]){
        Scanner kr=new Scanner(System.in);
        int num=kr.nextInt();
        int rem,rev=0;
        if(num>0){
            while(num>0){
                rem=num%10;
                rev=rev*10+rem;
                num=num/10;
                }
                System.out.println("Reversed number: "+rev);
            }
            else
            System.out.println("Error: Please enter a positive integer.");
    }
}
----------------------------------------------------------------------------------------------------------
EX 5(a) :
Imagine you're developing software for a weather monitoring station. You're tasked with
creating a program to analyze temperature data stored in an array of doubles. Your goal is to
determine the maximum temperature recorded and its corresponding time index.

coding:

import java.util.*;
class main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        double [] arr;
        int b=0;
        arr=new double[a];
        for(int i=0;i<a;i++)
    {
        arr[i]=sc.nextDouble();
        
    }
    double max=arr[0];
    for(int i=0;i<a;i++)
    {
        if(max<arr[i])
        {
            max=arr[i];
            b=i;
        }
    }
    System.out.println("Maximum Temperature: "+max);
    System.out.println("Index of Maximum Temperature: "+b);
    }
}
--------------------------------------------------------------------------------------------------------------------------
EX 5(b) :
You're developing software for a temperature monitoring system. Your task is to create a
program that analyzes temperature data stored in an array of integers. The program finds
the highest recorded temperature, along with its index.

coding:

import java.util.*;
class main
{
    public static void main(String []arg){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int []arr=new int[n];
        for(int i=0;i<n;i++)
        {
            arr[i]=sc.nextInt();
            
        }
        int max=arr[0];
        int index=0;
        for(int i=0;i<n;i++){
            if(arr[i]>max)
            {
                max=arr[i];
                index=i;
            }
        }
        System.out.println("Maximum Element: "+max);
        System.out.println("Index of Maximum Element: "+index);
    }
}
--------------------------------------------------------------------------------------------------------------------------------
EX 5(c) :
Suppose you are building a leaderboard for a gaming competition. You need to determine
the player who achieved the second-highest score. Develop a program to find the
second-highest score from an array of player scores. Ensure that your program handles
scenarios where there might be ties for the highest score and ensures that the
second-highest score is accurately identified. Test your program with various test cases to
verify its correctness.

coding:


import java.util.*;
class main{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int [] arr;
        int c=0;
        int temp;
        arr=new int [n];
        if(n<=1)
        
            System.out.println("-2147483648");
            else
            {
                for(int i=0;i<n;i++)
                {
                    arr[i]=sc.nextInt();
                }
                
                for(int i=0;i<n;i++)
                {
                     for(int j=i+1;j<n;j++){
                    if(arr[i]>arr[j])
                    {
                        temp=arr[i];
                        arr[i]=arr[j];
                        arr[j]=temp;
                    }
                }
                }
                int j=2;
                for(int i=0;i<n;i++)
                {
                    if(arr[n-1]==arr[n-j])
                    j++;
                    else
                    c=arr[n-j];
                }
                System.out.println(c);
            }
        }
    }

--------------------------------------------------------------------------------
EX 6(a) :
You're developing software for a financial analytics tool. Your task is to design a program to
process a dataset stored in a 2D array of integers. The program calculates the total value of
all entries in the dataset and prints the result.

coding:

import java.util.Scanner;
import java.util.*;
class SumOfArray{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        ArrayList<Integer>a=new ArrayList<>();
        while(in.hasNextInt())
        {
            int n=in.nextInt();
            a.add(n);
        }
        int x=0;
        for(int n:a){
            x+=n;
        }
        System.out.println("Sum of all elements in the 2D array: "+x);
    }
}
------------------------------------------------------------------------
EX 6(b) :
You're developing a data analysis tool for financial data. Your task is to develop a program to
process a dataset stored in a 2D array of doubles. The program identifies the highest value
in the dataset, along with its corresponding row and column indices.

coding:

import java.util.Scanner;
import java.util.*;
class Array{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        double [][] a = new double[n][m];
        int n1=0,m2=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                a[i][j]=in.nextDouble();
            }
        }
        double x =a[0][0];
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(a[i][j]>x){
                    x = a[i][j];
                    n1 = i;
                    m2 = j;
                }
            }
        }
        System.out.println("Maximum Element: "+x);
        System.out.println("Row Index of Maximum Element: "+n1);
        System.out.println("Column Index of Maximum Element: "+m2);
    }
}
-----------------------------------------------------------------------
EX 6(c) :
You're developing software for a scientific data analysis tool. Your task is to design a
program to process matrices. The program initializes a 2D array representing a matrix and
computes its transpose, providing both the original and transposed matrices for further
analysis.

coding:

import java.util.Scanner;
import java.util.*;
class mat{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        int row = in.nextInt();
        int col = in.nextInt();
        int [][] a = new int[row][col];
        System.out.println("Original Matrix:");
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                a[i][j] = in.nextInt();
            System.out.print(a[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println("Transposed Matrix:");
        for(int i=0;i<col;i++){
            for(int j=0;j<row;j++){
            System.out.print(a[j][i]+" ");
            }
            System.out.println();
        }
    }
}

......................................................................................

-------------------------------------------------------------------------
EX 7(a) :
As a linguistic data analyst, you're developing a Text Refinement Tool to process text inputs.
Users expect the tool to remove duplicate characters and count occurrences of vowels for
linguistic analysis. The tool accepts a string as input and redefines it, presenting the refined
text without duplicate characters and providing counts of vowels. Design a program that
meets these requirements efficiently.

coding:

import java.util.*;
class countVowel{
    public static void remove(String str){
        String newstr=new String();
        int length = str.length();
        char b=' ';
        for(int i=0;i<length;i++){
            b=str.charAt(i);
            if(newstr.indexOf(b)<0){
                if(b==' ')
                continue;
                
                
                else
                newstr+=b;
            }
        }
        System.out.println(newstr);
    }
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        String a=sc.nextLine();
        String b=a.toLowerCase();
        int c=0,e=0,j=0,o=0,u=0,n=0;
        for(int i=b.length()-1;i>=0;i--){
            char d=b.charAt(i);
            if(d=='a')
            c++;
            else if(d=='e')
            e++;
            else if(d=='i')
            j++;
            else if(d=='o')
            o++;
            else if(d=='u')
            u++;
            else
            n++;
        }
        if(n==b.length()){
            remove(a);
            System.out.println("No vowels found.");
        }
        else{
            remove(a);
            if(c>0)
            System.out.println("a"+c);
            if(e>0)
            System.out.println("e"+e);
            if(j>0)
            System.out.println("i"+j);
            if(o>0)
            System.out.println("o"+o);
            if(u>0)
            System.out.println("u"+u);
        }
    }
}
------------------------------------------------------------------------

EX 7(b) :
You are tasked with enhancing a popular text-editing app with a new feature to stylize text for
a more captivating presentation. With the app's extensive user base, efficiency and
performance are critical considerations. Users desire a feature that reverses the word order
of their input sentence while alternating the case of each letter, starting with uppercase for a
visually engaging effect.

coding:

import java.util.*;
class str{
    public static String convert(String str){
        int ln=str.length();
        String n="";
        for(int i=0;i<ln;i++){
            char c = str.charAt(i);
            if(c>='a'&&c<='z')
            n+=Character.toUpperCase(c);
            else if(c==' ')
            n+=" ";
            else
            n+=Character.toLowerCase(c);
        }
        return n;
    }
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        String a=sc.nextLine();
        String b=convert(a);
        String[] arr=b.split(" ");
        for(int i=arr.length-1;i>=0;i--){
            System.out.print(arr[i]+" ");
        }
    }
}
-------------------------------------------------------------------------
EX 7(c) :
You're developing a tool that reverse a given string S, such that the reversed string does not
contain any repeated characters or spaces. The program should eliminate any duplicate
characters and spaces while reversing the string.

coding:

import java.util.*;
class main{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        int l = s.length()-1;
        int [] arr=new int[123];
        for(int i=0;i<123;i++){
            arr[i]=0;
        }
        for(int i=l;i>=0;i--){
        if((s.charAt(i)>=65 && s.charAt(i)<=91) || (s.charAt(i)>=97 && s.charAt(i)<=122)){
            if(arr[s.charAt(i)]==0){
                System.out.print(s.charAt(i));
                arr[s.charAt(i)]++;
            }
        }
        else if(s.charAt(i)==' '){
            continue;
        }
        else{
            System.out.println("Invalid input");
            return;
        }
        }
    }
}

...........................................................................
EX 9(a) :
Imagine you're developing a utility to determine users' current ages for a registration
platform. Develop a program that prompts users to input their birth year.

coding:


import java.util.Scanner;
import java.time.LocalDate;
public class AgeCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Initialize birth year variable
int birthYear = 0;

while (true) {

if (scanner.hasNextInt()) {
birthYear = scanner.nextInt();

if (birthYear >= 0) {
break;
} else {
System.out.println("Invalid input. Birth year cannot be negative.");
}
} else {

scanner.next();
System.out.println("Invalid input. Please enter a valid year.");
}
}

int currentYear = LocalDate.now().getYear();

int age = currentYear - birthYear;

System.out.println(+ age);

scanner.close();
}}


------------------------------------------------------------------------
EX 9(b) :
In a financial application, Your task is to implement a function to round transaction amounts
to the nearest integer to ensure accurate representation of monetary values in reports and
accounting records.

coding:
import java.util.*;
class RoundedNum{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        double x = in.nextDouble();
        System.out.println("Rounded number: "+Math.round(x));
    }
}
---------------------------------------------------------------------------
Leave a Comment