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

Array of Objects

This Java code defines a Book class with name and id properties. It initializes an array of Book objects and populates the name and id properties by prompting the user for input. It then loops through the Book array and calls the check method, passing a string, to see if the name matches and return true or false.

Uploaded by

Pranay Kinra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Array of Objects

This Java code defines a Book class with name and id properties. It initializes an array of Book objects and populates the name and id properties by prompting the user for input. It then loops through the Book array and calls the check method, passing a string, to see if the name matches and return true or false.

Uploaded by

Pranay Kinra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Scanner;
class Book
{
String name;
int id;
Boolean check( String
bname)
{
boolean result = false;
if(name.equals(bname))
{
System.out.println("yes");
result = true;
}
else
{
System.out.println("No");
result = false;
}
return result;}
public static void main(String
args[])
{Scanner in = new
Scanner(System.in);
String ch="B";

Book[] bookArray = new


Book[3];
for ( int i=0;
i<bookArray.length; i++)
{
bookArray[i]=new Book();
}
for(int i=0;
i<bookArray.length; i++)
{
System.out.println("Enter
book name ");
bookArray[i].name=
in.nextLine();
}
for(int i=0;
i<bookArray.length; i++)
{
System.out.println("Enter
book id");
bookArray[i].id=in.nextInt
();
}
for(int i=0;
i<bookArray.length; i++)
{Boolean s=
bookArray[i].check(ch);
if(s)
break; }}}

You might also like