Xplore Java HandsOn
Xplore Java HandsOn
Xplore Java HandsOn
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
int noOfYear;
noOfYear = sc.nextInt();
class Account {
private int id;
private double balance;
private double interestRate;
2
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Point
{
int x,y;
Point(int x,int y)
{
this.x=x;
this.y=y;
}
}
4
class Point
{
double x,y;
5
Point(double x, double y)
{
this.x=x;
this.y=y;
}
}
6
oddDocs[i]=new Document();
}
int k=0;
for(int i=0;i<docsArray.length;i++)
{
if(docsArray[i].pages%2!=0)
{
oddDocs[k++]=docsArray[i];
}
}
return oddDocs;
}
}
class Document
{
int id,pages;
String title,folderName;
}
Problem 2:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
}
for(int i=0;i<booksArray.length;i++)
{
booksArray[i].id=scn.nextInt();
booksArray[i].title=scn.next();
booksArray[i].author=scn.next();
booksArray[i].price=scn.nextDouble();
}
sorted=sortBooksByPrice(booksArray);
for(int i=0;i<sorted.length;i++)
{
System.out.println(sorted[i].id+" "+sorted[i].title+" "+sorted[i].author+"
"+sorted[i].price);
}
}
public static Book[] sortBooksByPrice(Book[]booksArray){
int n=booksArray.length;
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(booksArray[j].price>booksArray[j+1].price)
{
Book temp=booksArray[j];
booksArray[j]=booksArray[j+1];
booksArray[j+1]=temp;
}
}
}
return booksArray;
}
}
class Book
{
int id;
String title,author;
double price;
}
11
import java.util.Scanner;
for(int i = 0;i<5;i++)
{
int tag = sc.nextInt();sc.nextLine();
String brand = sc.nextLine();
double price = sc.nextDouble();sc.nextLine();
char g = sc.nextLine().charAt(0);
shirts[i] = new Shirt(tag,brand,price,g);
}
for(Shirt s: shirts)
{
System.out.println(getDiscountPrice(s));
}
for(Shirt s: result)
{
if(s.getTag()!=0)
System.out.println(s.getTag()+" "+s.getPrice()+ " " + s.getBrand());
}
12
}
public static Shirt[] getShirtWithMoreThanSpecificPrice(Shirt [] shirts, double price)
{
Shirt[] res = new Shirt[5];
for(int i=0;i<res.length;i++)
res[i]=new Shirt(0,"",0,'c');// taking sample to avoid null pointer exception
int j=0;
for(int i=0;i<shirts.length;i++)
{
if(shirts[i].price>price)
{
res[j++]= new Shirt(shirts[i].tag,shirts[i].brand,shirts[i].price,shirts[i].gender);
}
}
return res;
}
}
class Shirt
{
//define the class as per details shared in the question
int tag;
String brand;
13
double price;
char gender;
Shirt(int tag, String brand, double price, char gender)
{
this.tag=tag;
this.brand=brand;
this.price=price;
this.gender=gender;
}
public int getTag()
{
return this.tag;
}
public void setTag(int tag)
{
this.tag=tag;
}
public String getBrand()
{
return this.brand;
}
public void setBrand(String brand)
{
this.brand=brand;
}
public double getPrice()
{
return this.price;
}
public void setPrice(double price)
{
this.price=price;
}
public char getGender()
{
return this.gender;
}
public void setGender(char gender)
{
14
this.gender=gender;
}
}
Problem 2:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.*;
{
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()
{
16
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;
}