top of page
Java Arrays - Hands on 2
Problem 1:
​
Shirt Discount, Price
Class Solution has predefined main method to read values and display the results.
main method reads the value for five objects of class Shirt.
It also calls methods - getDiscountPrice and getShirtWithMoreThanSpecificPrice defined in same Solution class.
Code inside main method should not be altered else your solution might be scored as zero.
You may copy the code from main method in eclipse to verify your implementation.
Guidelines to implement the code:
Create class Shirt with attributes:
int tag
String brand
double price
char gender
Create constructor which takes parameters in above sequence. Create getters and setters for these attributes.
Next, in Solution class, define two static methods as below:
public static double getDiscountPrice(Shirt s):
This method will return the discounted price based on gender for the Shirt object which is passed as input parameter.
If gender is 'm' then discount is 10%. For 'f' it is 20% and for 'u' it is 30%.
public static Shirt[] getShirtWithMoreThanSpecificPrice(Shirt[] shirts,double price):
This method will take array of Shirt objects and price value.
The method will return array of Shirt objects with more than specified price in ascending order of price.
Main method already has Scanner code to read values, create objects and test above methods.
Main method reads values for five Shirt objects, followed by price.
Price will be input for method getShirtWithMoreThanSpecificPrice.
You can consider below sample input and output to verify your implementation before submitting in Hackerrank.
Sample input:
1
arrow
500
m
2
bare
600
f
3
arrow
400
m
4
bare
300
m
5
arrow
1000
u
500
Sample output:
450.0
480.0
360.0
270.0
700.0
2 600.0 bare
5 1000.0 arrow
​
Solutions:
​
import java.util.Scanner;
public class Solution {
public static void main(String args[] ) throws Exception {
/* Do not alter code in main method */
Shirt[] shirts = new Shirt[5];
Scanner sc = new Scanner(System.in);
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);
}
double price = sc.nextDouble();
for(Shirt s: shirts)
{
System.out.println(getDiscountPrice(s));
}
Shirt[] result = getShirtWithMoreThanSpecificPrice(shirts,price);
for(Shirt s: result)
{ if(s.tag!=0)
System.out.println(s.tag+" "+s.price+ " " + s.brand);
}
}
public static Double getDiscountPrice(Shirt s){
char ge=s.g;
int f=0;
if(ge=='m')
f=10;
if(ge=='f')
f=20;
if(ge=='u')
f=30;
double p=s.price;
return p-((p*f)/100);
}
public static Shirt[] getShirtWithMoreThanSpecificPrice(Shirt[] shirts,double price){
Shirt[] r=new Shirt[5];
for(int i=0;i<r.length;i++){
r[i]=new Shirt(0,"",0.0,'f');
}
int k=0;
for(int i=0;i<r.length;i++){
if(shirts[i].price>price)
r[k++]=shirts[i];
}
int n=r.length;
for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;j++){
if(r[j].price>r[j+1].price){
Shirt t=r[j];
r[j]=r[j+1];
r[j+1]=t;
}
}
}
return r;
}
}
class Shirt
{ int tag;
String brand;
double price;
char g;
Shirt(int tag,String brand,double price, char g){
this.tag=tag;
this.brand=brand;
this.price=price;
this.g=g;
}
}
​
Java p1 Arrays - Hands on: About
bottom of page