LPU Java Unit 1 Lecture 4 COD Basics RB | LPUCOLAB

LPU Java Unit 1 Lecture 4 COD Basics RB | LPUCOLAB

LPU_Java_Unit 1_Lecture 4_COD_Basics_RB

1. Problem Statement

Ram is a data scientist who is preparing a report on AI model training for their team's daily meeting. He wants to create a program that reads the model's name, training epochs, batch size, learning rate, and whether the training was successful, then displays a formatted summary for the meeting.

Can you assist Ram in this?

Input format:

The input consists of: - Model Name (String) - Training Epochs (int) - Batch Size (int) - Learning Rate (float) - Training Success Status (boolean)

Output format:

The first line of output prints the model name.

The second line prints the number of training epochs.

The third line prints the batch size.

The fourth line prints the learning rate, rounded off to two decimal places.

The fifth line prints the training success status.

Solution:

import java.util.Scanner;
import java.text.DecimalFormat;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String modelName = scanner.nextLine();
        int epochs = scanner.nextInt();
        int batchSize = scanner.nextInt();
        double learningRate = scanner.nextDouble();
        boolean success = scanner.nextBoolean();

        DecimalFormat df = new DecimalFormat("#.##");

        System.out.println("Model Name: " + modelName);
        System.out.println("Training Epochs: " + epochs);
        System.out.println("Batch Size: " + batchSize);
        System.out.println("Learning Rate: " + df.format(learningRate));
        System.out.println("Training Successful: " + success);
    }
}

2. Problem Statement

Sam, the esteemed captain of his sports team, desires to write a program that displays the name of his team. The program should prompt Sam for the team’s name and subsequently present it in a refined manner.

Can you assist Sam in this task?

Input format:

The input consists of a string, which can include letters, numbers, spaces, and special characters.

Output format:

The output prints:
Sam's team name is: followed by the team name.

Solution:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String teamName = scanner.nextLine();
        System.out.println("Sam's team name is: " + teamName);
    }
}

3. Problem Statement

Sarah, a software developer, is working on a project to create a program that generates personalized welcome messages for users of a chat application. She wants to design a simple program where users can input their names, and the program will display a welcoming message for them.

Your task is to help Sarah in the project.

Input format:

The input consists of a string representing the name of the user.

Output format:

The output prints:
Welcome, X! where X is the user input string.

Solution:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("Welcome, " + name + "!");
    }
}