0

Not long ago, I had an online class with my lecturer. We did an exercise together but for this exercise, even when she showed all the coding in said exercise, I got some errors when before I could run it. For errors, one is a red and the other is a yellow.

The exercise is about design a form for hotel staffs to calculate fees of their guests' stay if I recall correctly.

This is the main class. The red line error I got here is "Scanner scanner = new Scanner(System.in);". I'm not familiar with scanner use in java btw so I don't know what I'm missing here.

   public class TestHotel12102020 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Hotel h = new Hotel();
        Suite s = new Suite();
        
        
        System.out.println("Enter room number");
        int room = scanner.nextInt();
        System.out.println("Days");
        int r = scanner.nextInt();
        System.out.println("Enter type of room number : Suite @ regulaer");
        String type = scanner.next;
        
        if (type.equalsIgnoreCase("Suite")) {
            s.setRoom(room);
            s.setDays(r);
            s.calculateBill();
        }
        
        else {
            h.setRoom(room);
            h.setDays(r);
            h.calculateBill();
        
    }
  }   
}

As for this class, the yellow line error (caution symbol) is at "void calculateBill() {"

    public class Suite extends Hotel {
    
    double rental2;
    
    void calculateBill() {
        if (super.getRoom() <= 299) // encapsulation
            rental2 = 70*super.getDays()+40;
        else
            rental2 = 90*super.getDays()+40;
        
        System.out.println("Your rental fee is " + rental2);
    }    
}

This last class is all fine but since I typed calculationBill function here, I believe this could relate with the previous class where I got the yellow line error.

    public class Hotel extends TestHotel12102020 {
    
    private int roomnumber;
    private int days;
    double bill;
    
    public void setRoom(int room) {
        roomnumber = room;
    }
    
    int getRoom() {
        return roomnumber;
    }
    
    public void setDays(int r) {
        days = r;
    }
    
    double getDays() {
        return days;
    }
    
    void calculateBill() {
        if (roomnumber <= 299)
            bill = 70*days;
        
        else
            bill = 90*days;
        System.out.println("Your bill is " + bill);
    }

}

I copied all my lecturer's codes thoroughly during the online class but when I checked hers and mine before class was over, I checked she had no errors in any of her code lines but mine got some so I felt confused. Besides, I'm still a noob at java so I hope you can properly guide me in this and I'd appreciate it if you do so.

P.S. : Do you know any useful sites doing online exercises for java?

John Joe
  • 12,412
  • 16
  • 70
  • 135
YN27
  • 33
  • 6

1 Answers1

0

You miss import Scanner.

import java.util.Scanner;  // Import the Scanner class
John Joe
  • 12,412
  • 16
  • 70
  • 135
  • So I followed what you said and ran the program. Luckily, it went smooth but is it okay to leave the yellow line error; void calculateBill(), untouched? I just feel a little worried – YN27 Oct 18 '20 at 12:42
  • Is the output is expected output? – John Joe Oct 20 '20 at 16:18