Lab 3 (Solved)

Unit 3 -s Using Java Classes

Posted on 3/5/2025

Question 1

import java.util.Scanner;

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

        System.out.println("Please enter your name:");
        String name = scanner.nextLine();

        System.out.println("Please enter your age:");
        int age = scanner.nextInt();
        scanner.nextLine();
        System.out.println("Please enter your country of birth:");
        String country = scanner.nextLine();

        System.out.println("\nHi " + name + ", you're " + age + " and born in " + country);
    }
}

Question 2

import java.util.Scanner;

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

        System.out.println("Please enter your fullname:");
        String fullName = scanner.nextLine();

        String upperCaseName = fullName.toUpperCase();
        String lowerCaseName = fullName.toLowerCase();
        int nameLength = fullName.length();

        System.out.println("Your name in uppercase is: " + upperCaseName);
        System.out.println("Your name in lowercase is: " + lowerCaseName);
        System.out.println("The length of your name is: " + nameLength);
    }
}

Question 3

import java.util.Scanner;

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

        System.out.println("Please enter your fullname:");
        String fullName = scanner.nextLine();

        char firstInitial = fullName.charAt(0);
        int spaceIndex = fullName.indexOf(' ');
        char lastInitial = fullName.charAt(spaceIndex + 1);
        String familyName = fullName.substring(spaceIndex + 1);

        System.out.println("Your initials are: " + firstInitial + "." + lastInitial + ".");
        System.out.println("Your family name is: " + familyName);
    }
}

Question 4

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

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

        System.out.println("Please enter the radius of the circle: ");
        double radius = scanner.nextDouble();

        double circumference = 2 * Math.PI * radius;
        double area = Math.PI * Math.pow(radius, 2);

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

        System.out.println("The circumference of the circle is: " + df.format(circumference));
        System.out.println("The area of the circle is: " + df.format(area));
    }
}

Question 5

import java.util.Scanner;

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

        System.out.println("Please enter the ISBN in the following format x-xxx-xxxxx-x: ");
        String isbn = scanner.nextLine();
        Scanner isbnScanner = new Scanner(isbn);
        isbnScanner.useDelimiter("-");
        String language = isbnScanner.next();
        String publisher = isbnScanner.next();
        String book = isbnScanner.next();
        String check = isbnScanner.next();
        System.out.println("Language is: " + language);
        System.out.println("Publisher is: " + publisher);
        System.out.println("Book is: " + book);
        System.out.println("Check is: " + check);

        scanner.close();
        isbnScanner.close();
    }
}