0% found this document useful (0 votes)
86 views

Import Java - util.ArrayList

The document defines a ToDoList program with the following functionality: 1. It allows the user to add tasks to a to-do list by prompting for a description. 2. The user can mark existing tasks as completed by entering the task number. 3. All current tasks are displayed with their description and completion status. 4. The program exits when the user selects the exit option from the main menu.

Uploaded by

Ronel Leboutou
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

Import Java - util.ArrayList

The document defines a ToDoList program with the following functionality: 1. It allows the user to add tasks to a to-do list by prompting for a description. 2. The user can mark existing tasks as completed by entering the task number. 3. All current tasks are displayed with their description and completion status. 4. The program exits when the user selects the exit option from the main menu.

Uploaded by

Ronel Leboutou
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

import java.util.

ArrayList;
import java.util.Scanner;

class Task {

private String description;


private boolean completed;

public Task(String description) {


this.description = description;

this.completed = false;
}

public void markAsCompleted() {


completed =
completed

true;
}

public String getDescription() {


return description;
}

public boolean isCompleted() {

return completed;
}
}

public class ToDoList {

public static void main(String[] args) {

Scanner scanner
Scanner scanner = new Scanner(System.in);
ArrayList<Task> tasks =
ArrayList<Task> tasks =

ArrayList<Task> tasks

new ArrayList<>();

while (true) {
System.out.println(
System.out.println

System
"1. Add Task");
System.out.println(
System.out.println

System.out

System
"2. Mark Task as Completed");
System.out.println("3. List Tasks");
System.out.println("4. Exit");
System.out.print(
System.out
"Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine();
scanner.nextLine();

scanner.nextLine

scanner

// Consume newline

switch (choice) {
case 1:
System.out.print(
System.out.print

System.out

System

"Enter task description: ");

String description
String description = scanner.nextLine();
tasks.add(
tasks.add
new Task(description));
break;
case 2:
System.out.print(
System.out.print

System.out
"Enter task number to mark as completed: ");

int taskNumber = scanner.nextInt();


if (taskNumber >= 1 && taskNumber <= tasks.size()) {
tasks.get(taskNumber -
tasks.get(taskNumber -

tasks.get(taskNumber

tasks.get(task
tasks.get

1).markAsCompleted();
} else {
System.out.println(
System.out.println
"Invalid task number.");
}

}
break;

case 3:
System.out.println(
System

"Tasks:");
for (int i = 0; i < tasks.size(); i++) {

Task
Task task = tasks.get(i);
System.out.println((i +
System.out.println((

System.out.println

System
1) + ". " + task.getDescription() + " - " +
(task.isCompleted() ?
(task.isCompleted

(task.is

(task
"Completed" : "Not Completed"));
}

}
break;

case 4:
System.out.println(
System.out.println

System.out
"Exiting...");
scanner.close();
System.exit(
scanner.close();
System.exit(

scanner.close();
scanner.close
0);

default:
System.out.println(
System.out
"Invalid choice.");
}
}
}
}

}
}
}
}
``

}
}
}

}
}

Exemple
Java

Copier le code
import java.util.Scanner;

public class SimpleCalculator {


public static void main(String[] args) {

Scanner scanner

Scanner

Scanner scanner = new Scanner(System.in);

System.out.print(

System.out.print

System.out

System
"Enter first number: ");
double num1 = scanner.nextDouble();

System.out.print(

System.out.print

System.out

"Enter second number: ");

double num2 = scanner.nextDouble();

System.out.println(

System.out.println

System.out

System

"Select operation (+, -, *, /): ");

char operation = scanner.next().charAt(0);

double result = 0;

switch (operation) {

case '+':
result = num1 + num2;

result = num1 + num2;

result = num1 + num2;

result = num1 + num2


result = num1 + num

result = num1 +

result = num

break;

case '-':
result = num1 - num2;

result = num1 - num2;

result = num1 - num2;

result = num1 - num2

result = num1 - num

result = num1 -

result = num1

result =

result

break;

case '*':
result = num1 * num2;

result = num1 * num2;

result = num1 * num

result = num1 *

result = num1

result = num

result

break;

case '/':

if (num2 != 0) {
result = num1 / num2;
}
result = num1 / num2;
}

result = num1 / num2;

result = num1 / num2

result = num1 / num

result = num1
else {
System.out.println(
System.out

System

"Error: Division by zero.");


System.exit(
System.exit

1);
}

break;

default:
System.out.println(
System.out.println

System.out

"Invalid operation.");
System.exit(
System.exit(

System.exit

System

1);
}

System.out.println(
}

System.out

}
"Result: " + result);

scanner.close();
}
}

scanner.close();
}
}
``

scanner.close();
}
}

scanner.close();
}

scanner.close();

scanner.close();

scanner.close

Ex
Java

Copier le code
import java.util.ArrayList;
import java.util.Scanner;

class

class Book {

private String title;

private String author;

public Book(String title, String author) {


this.title = title;
this.author = author;
}

public String getTitle() {

return title;
}

public String getAuthor() {

return author;
}
}

public class LibraryManager {


public static void main(String[] args) {

Scanner scanner

Scanner
Scanner scanner = new Scanner(System.in);
ArrayList<Book> books =
ArrayList<Book> books

ArrayList<Book>

ArrayList<Book

ArrayList

new ArrayList<>();
while (true) {
System.out.println(
System.out.println

System.out

"1. Add Book");


System.out.println(
System.out

System

"2. List Books");


System.out.println(
System.out.println

System.out

System

"3. Exit");
System.out.print(
System.out.print

System.out

System

"Choose an option: ");

int choice = scanner.nextInt();


scanner.nextLine();
scanner.nextLine();

scanner.nextLine

scanner

// Consume newline

switch (choice) {

case 1:
System.out.print(
System.out.print
System.out

System

"Enter book title: ");

String title

String
String title = scanner.nextLine();
System.out.print(
System.out.print

System.out

System

"Enter book author: ");

String author

String

String author = scanner.nextLine();


books.add(
books.add

new Book(title, author));


break;
case 2:
System.out.println(
System.out
"Books in Library:");

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


Book book = books.get(i);
System.out.println((i +
System.out.println((i +

System.out.println((i +

System.out.println((i

System.out.println
1) + ". " + book.getTitle() + " by " + book.getAuthor());
}

break;
case 3:
System.out.println("Exiting...");
scanner.close();
System.exit(
scanner.close();
System.exit
0);
default:
System.out.println(
System
"Invalid choice.");
}
}
}
}

You might also like