Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java class representing the concept of Hotel Rating over a number of yea

ID: 3860379 • Letter: W

Question

Write a Java class representing the concept of Hotel Rating over a number of years. The hotel ratings are represented by a number of stars: one star (lowest quality) to five stars (highest quality). The only attribute the class has is a two-dimensional array of values representing the quality ratings. Dimension 1 (rows) represents the hotels and dimension 2 (columns) represents the years. Write the following class methods:

A constructor method that takes two parameters representing the number of hotels and the number of years. The ratings are randomly generated and stored in the array.

Method bestHotel that returns an array of 2 elements holding the index for the best quality hotel over the years and the index for the worst quality hotel over the years.

Method printChart that prints a rating chart for all hotels similar to the following sample output (just for illustration, assume we have 3 hotels and 4 years of ratings):

Hotel 0:    **          ***         ****       ***

Hotel 1:    ***       ***         **          **

Hotel 2:    *****       ****       ****       *****

Write a test program to create an object of the class and test all of the class methods on that objects.

Explanation / Answer

TestProgram.java

public class TestProgram {

public static void main(String args[]){
HotelRatings hotelRatings=new HotelRatings(0,2);
hotelRatings.printChart();
int arr[]=hotelRatings.bestHotel();
System.out.println("Index of best rating hotel: "+arr[0]);
System.out.println("Index of worst rating hotel: "+arr[1]);

}
}


HotelRatings.java

public class HotelRatings {

//The 2-D array that will store hotel ratings
String ratingsArr[][];

//Constructor that initializes number of hotels and years
public HotelRatings(int n,int m){
ratingsArr=new String[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
//Randomly generating ratings
int rating =(int) ((Math.random()*100 %5) +1);

/*Converting the integer rating to Stars*/
switch(rating){
case 1:
ratingsArr[i][j]="*";
break;
case 2:
ratingsArr[i][j]="**";
break;
case 3:
ratingsArr[i][j]="***";
break;
case 4:
ratingsArr[i][j]="****";
break;
case 5:
ratingsArr[i][j]="*****";
break;
default:
break;
}
}
}
}
public void printChart(){
for(int i=0;i<ratingsArr.length;i++){
System.out.print("Hotel "+i +": ");
for(int j=0;j<ratingsArr[i].length;j++){
System.out.print(ratingsArr[i][j]+" ");
}
System.out.println(" ");
}
}

public int[] bestHotel(){
int lowestRating=0, highestRating=0;
int lowestRatingHotelIndex=0, highestRatingHotelIndex=0;
for(int i=0;i<ratingsArr.length;i++){
int rating=0;
for(int j=0;j<ratingsArr[i].length;j++){
rating+=ratingsArr[i][j].length();
}
if(i==0){
highestRatingHotelIndex=lowestRatingHotelIndex=i;
highestRating=lowestRating=rating;
}else{
if(rating<lowestRating){
lowestRating=rating;
lowestRatingHotelIndex=i;
}
else if(rating>highestRating){
highestRating=rating;
highestRatingHotelIndex=i;
}
}

}
return new int[]{highestRatingHotelIndex,lowestRatingHotelIndex};
}
}

Sample Output:
Hotel 0: **** ** ** **

Hotel 1: * ** *** *****

Hotel 2: *** ** ***** ***

Index of best rating hotel: 2
Index of worst rating hotel: 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote