Buffl

java

En
von EYAD N.

Folder 1

package eyad;

package eyad;

public class main {

public static void main(String[] args) {


Book myBook = new Book();

myBook.displayBookInfo(" OOP java", "eyad", "9780446310789");


System.out.println("-------------------------");


Book myBook2 = new Book();

myBook2.title="OOP java";

myBook2.author="eyad";

myBook2.isbn="9780446310789";

myBook2.text();

}}


}}

Folder 2

package eyad;

public class Book {

String title;

String author;

String isbn;

public void displayBookInfo(String title, String author, String isbn) {


System.out.println("Book Details:");


System.out.println("Title: " + title);


System.out.println("Author: " + author);


System.out.println("ISBN: " + isbn);


}

public void text () {


System.out.println("Book Details:");


System.out.println("logo: " + title);


System.out.println("name: " + author);


System.out.println("id: " + isbn);

}}

THE OUTPUT WILL BE

Book Details:

Title: OOP java

Author: eyad

ISBN: 9780446310789

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

Book Details:

logo: OOP java

name: eyad

id: 9780446310789

——————————————————————————————————————

——————————————————————————————————————

folder 1

package eyad;

public class main {

public static void main(String[] args) {


Book myBook = new Book(" OOP java", "eyad", "9780446310789");


myBook.author="eyad";

myBook.displayBookInfo();

}}

folder 2

package eyad;

public class Book {

// Instance variables (attributes) of the Book

String title;

String author;

String isbn;

// Constructor to initialize the Book object

public Book(String title, String author, String isbn) {

this.title = title;

this.author = author;

this.isbn = isbn;

}

// Method to display book information

public void displayBookInfo() {

System.out.println("Book Details:");

System.out.println("Title: " + title);

System.out.println("Author: " + author);

System.out.println("ISBN: " + isbn);

}}

THE OUTPUT WILL BE

Book Details:

Title: OOP java

Author: eyad

ISBN: 9780446310789


What will be the output of this code

  1. class Hello { public static void main (String[] args) {

int a = 1;

switch (a) {

case 1: System.out.println ("One");

case 2: System.out.println ("Two");

case 3: System.out.println ("Three");

default: System.out.println ("Default");

}}

}

  1. What will be the output if int x =2 instead of 1?

  1. One

    Two

    Three

    Default

_________________

  1. Two

    Three

    Default

Explanation

The switch statement in your code is missing break statements after each case. Without these break statements, the program will fall through and execute all subsequent case blocks, including the default block. Here's the behavior of the given code:

Output of the code:

One

Two

Three

Default

Why this happens:

int a = 1; assigns the value 1 to a.

The switch statement evaluates a and matches it to case 1.

Since there’s no break after case 1, the program continues executing all subsequent case blocks and the default block.

Correcting the code:

If you want the program to stop executing further cases after the matched one, add break statements:

class Hello {

public static void main(String[] args) {

int a = 1;

switch (a) {

case 1:

System.out.println("One");

break;

case 2:

System.out.println("Two");

break;

case 3:

System.out.println("Three");

break;

default:

System.out.println("Default");

}

}

}

Corrected Output:

One

Notes:

break is used to exit the switch statement after executing a specific case.

If you intentionally want multiple cases to execute, you can omit break, but this should be done with caution to avoid unintended behavior.

Author

EYAD N.

Informationen

Zuletzt geändert