You are on page 1of 6

TRƯỜNG ĐẠI HỌC HỌC VĂN LANG

KHOA CÔNG NGHỆ THÔNG TIN

TÀI LIỆU THỰC HÀNH

CƠ SỞ LẬP TRÌNH
(CTĐTDB)

2024
Van Trung NGUYEN - Faculty of Information Technology

EXERCISES
Exercise 1:

import java.util.Scanner;

public class ArithmeticExample {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter first number (a): ");

int a = scanner.nextInt();

System.out.print("Enter second number (b): ");

int b = scanner.nextInt();

System.out.println("a + b = " + (a + b));

System.out.println("a - b = " + (a - b));

System.out.println("a * b = " + (a * b));

System.out.println("a / b = " + (a / b));

Exercise 2:

Programming basics 2
Van Trung NGUYEN - Faculty of Information Technology

import java.util.Scanner;

public class RelationalExample {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number (a): ");
int a = scanner.nextInt();
System.out.print("Enter second number (b): ");
int b = scanner.nextInt();

System.out.println("a > b is " + (a > b));


System.out.println("a < b is " + (a < b));
}
}

Exercise 3:

import java.util.Scanner;

public class LogicalExample {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter first boolean value (x, true or false): ");

boolean x = scanner.nextBoolean();

System.out.print("Enter second boolean value (y, true or false): ");

boolean y = scanner.nextBoolean();

System.out.println("x && y = " + (x && y));

System.out.println("x || y = " + (x || y));

Programming basics 3
Van Trung NGUYEN - Faculty of Information Technology

Exercise 4:

import java.util.Scanner;

public class UnaryExample {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number (a): ");

int a = scanner.nextInt();

System.out.println("Original value of a: " + a);

System.out.println("After incrementing: " + (++a));

System.out.println("After decrementing: " + (--a));

Exercise 5:

import java.util.Scanner;

public class AssignmentExample {

Programming basics 4
Van Trung NGUYEN - Faculty of Information Technology

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter first number (a): ");

int a = scanner.nextInt();

System.out.print("Enter second number (b): ");

int b = scanner.nextInt();

a += b; // a = a + b

System.out.println("After addition assignment, a = " + a);

a -= b; // a = a - b

System.out.println("After subtraction assignment, a = " + a);

Exercise 6: Let users enter a score of 3 subjects. Let's calculate the GPA and output

the results

import java.util.Scanner;

public class AverageScore {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Prompting user to enter scores for three subjects

Programming basics 5
Van Trung NGUYEN - Faculty of Information Technology

System.out.print("Enter score for subject 1: ");

double score1 = scanner.nextDouble();

System.out.print("Enter score for subject 2: ");

double score2 = scanner.nextDouble();

System.out.print("Enter score for subject 3: ");

double score3 = scanner.nextDouble();

// Calculating the average of the scores

double average = (score1 + score2 + score3) / 3;

// Outputting the result

System.out.println("The average score is: " + average);

Exercise 7: Let users enter value of a, b, c, d, e, g, h variables and output the results:

---END---

Programming basics 6

You might also like