Super Cool Bucket
Super Cool Bucket

Reputation: 33

Checking availability of Getters and Setters in Java

I have a class Book with private variables bookName(type:String), bookPrice (type:int),authorName(type:String).I have included appropriate getters and setters methods for these private variables.

Another class TestBook created which has main method.I need to get the details bookName,bookPrice,authorName from user.

Object for book class created in Testbook class and assign the value for its attributes using the setters. Also to print the output output using the getters method.

I wrote the entire code but getting failure.

Fail1 : Check availability of getter/setter of bookName attribute Fail2 : Check availability of getter/setter of bookPrice attribute Fail3 : check availbility of getter/setter of authorName attribute

Book.java

 public class  Book {

         private String bookName;
         private int bookPrice;
         private String authorName;

         //getters

         public String getbookName(){
             return bookName;
         }

        public String getauthorName(){
              return authorName;
        }
        public int getbookPrice(){
            return bookPrice;
        }

       //setters


        public void setbookName(String newName){
             this.bookName = newName;
        }
        public void setbookPrice(int bokPrice){
            this.bookPrice = bokPrice;
        }
        public void setauthorName(String author){
            this.authorName = author;
        }


    }

TestBook.java

 import java.util.Scanner;

     class TestBook {

         public static void main(String[] args){
         Scanner my = new Scanner(System.in);

         String name1,name2;
         int num;

        // Getting user input

        System.out.println("Enter the Book name:");
        name1 = my.nextLine();

        System.out.println("Enter the price:");
        num = my.nextInt();

        String re = my.nextLine();

        System.out.println("Enter the Author name:");
        name2 = my.nextLine();

        //Setting values of private variable using setters

        Book myObj = new Book();
        myObj.setbookName(name1);
        myObj.setbookPrice(num);
        myObj.setauthorName(name2);

        //Printing values using getters

        System.out.println("Book Details");
        System.out.println("Book Name :"+myObj.getbookName());
        System.out.println("Book Price :"+myObj.getbookPrice());
        System.out.println("Author Name :"+myObj.getauthorName());



        }
    }

Upvotes: 0

Views: 5061

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201437

getbookName() should be getBookName(). setbookName() should be setBookName() and so on, for all of your getter(s) and setter(s). That is the correct Java convention. And I can only assume the criteria your tester is looking for.

Upvotes: 3

Related Questions