JAVA handns on 2 problem 2
Problem 2:
_________________________________
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.*;
public class Solution {
public static void main(String args[] ) throws Exception {
Scanner scn=new Scanner(System.in);
Book[] booksArray=new Book[4];
Book[] res=new Book[4];
for(int i=0;i<booksArray.length;i++)
{
booksArray[i]=new Book();
res[i]=new Book();
}
for(int i = 0;i<4;i++)
{
booksArray[i].id = scn.nextInt();scn.nextLine();
booksArray[i].title = scn.nextLine();
booksArray[i].author = scn.nextLine();
booksArray[i].price = scn.nextDouble();
}
String value=scn.next();
res=searchTitle(value, booksArray);
int [] matchedId=new int[4];
int j=0;
for(int i=0;i<res.length;i++)
{
if(res[i].id!=0)
{
matchedId[j++]=res[i].id;
}
}
Arrays.sort(matchedId);
for(int i=0;i<matchedId.length;i++)
{
if(matchedId[i]!=0)
System.out.println(matchedId[i]);
}
}
public static Book[] searchTitle(String value, Book[] books)
{
int k=0;
Book[] matching=new Book[4];
for(int i=0;i<matching.length;i++)
matching[i]=new Book();
for(int i=0;i<books.length;i++)
{
String val=value.toLowerCase();
String bookTitle=books[i].title.toLowerCase();
if(bookTitle.contains(val))
{
matching[k++]=books[i];
}
}
return matching;
}
}
class Book
{
int id;
String title;
String author;
double price;
public int getId()
{
return this.id;
}
public void setId(int id)
{
this.id=id;
}
public String getTitle()
{
return this.title;
}
public void setTitle(String title)
{
this.title=title;
}
public String getAuthor()
{
return this.author;
}
public void setAuthor(String author)
{
this.author=author;
}
public double getPrice()
{
return this.price;
}
public void setPrice(double price)
{
this.price=price;
}
Problem 2:
_________________________________
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.*;
public class Solution {
public static void main(String args[] ) throws Exception {
Scanner scn=new Scanner(System.in);
Book[] booksArray=new Book[4];
Book[] res=new Book[4];
for(int i=0;i<booksArray.length;i++)
{
booksArray[i]=new Book();
res[i]=new Book();
}
for(int i = 0;i<4;i++)
{
booksArray[i].id = scn.nextInt();scn.nextLine();
booksArray[i].title = scn.nextLine();
booksArray[i].author = scn.nextLine();
booksArray[i].price = scn.nextDouble();
}
String value=scn.next();
res=searchTitle(value, booksArray);
int [] matchedId=new int[4];
int j=0;
for(int i=0;i<res.length;i++)
{
if(res[i].id!=0)
{
matchedId[j++]=res[i].id;
}
}
Arrays.sort(matchedId);
for(int i=0;i<matchedId.length;i++)
{
if(matchedId[i]!=0)
System.out.println(matchedId[i]);
}
}
public static Book[] searchTitle(String value, Book[] books)
{
int k=0;
Book[] matching=new Book[4];
for(int i=0;i<matching.length;i++)
matching[i]=new Book();
for(int i=0;i<books.length;i++)
{
String val=value.toLowerCase();
String bookTitle=books[i].title.toLowerCase();
if(bookTitle.contains(val))
{
matching[k++]=books[i];
}
}
return matching;
}
}
class Book
{
int id;
String title;
String author;
double price;
public int getId()
{
return this.id;
}
public void setId(int id)
{
this.id=id;
}
public String getTitle()
{
return this.title;
}
public void setTitle(String title)
{
this.title=title;
}
public String getAuthor()
{
return this.author;
}
public void setAuthor(String author)
{
this.author=author;
}
public double getPrice()
{
return this.price;
}
public void setPrice(double price)
{
this.price=price;
}
No comments:
Post a Comment