Observe the below class and match the output for the code snippets provided.public class Product{ //Attributes private int productId; private String productName; private float price; private char category; //Constructor public Product(){ } public Product(int productId,String productName,float price) { this.productId=productId; this.productName=productName; this.price=price; } public Product(int productId,String productName,float price,char category) { this(productId,productName,price); this.category=category; } public void display() { System.out.println(productId+" "+productName+" "+price+" "+category); } }Product p = new Product()p.display();Answer 1Product p = new Product(71,"Mobile",80000,'A');p.display();Answer 2Product p = new Product(501,"Wallet",520);p.display();Answer 3
Question
Observe the below class and match the output for the code snippets provided.public class Product{ //Attributes private int productId; private String productName; private float price; private char category; //Constructor public Product(){ } public Product(int productId,String productName,float price) { this.productId=productId; this.productName=productName; this.price=price; } public Product(int productId,String productName,float price,char category) { this(productId,productName,price); this.category=category; } public void display() { System.out.println(productId+" "+productName+" "+price+" "+category); } }Product p = new Product()p.display();Answer 1Product p = new Product(71,"Mobile",80000,'A');p.display();Answer 2Product p = new Product(501,"Wallet",520);p.display();Answer 3
Solution 1
Sure, here are the answers to your question:
Answer 1: The output will be "0 null 0.0" because you are creating an object of the Product class using the default constructor which doesn't initialize any values to the attributes.
Answer 2: The output will be "71 Mobile 80000.0 A" because you are creating an object of the Product class using the parameterized constructor which initializes the values of productId, productName, price, and category.
Answer 3: The output will be "501 Wallet 520.0" because you are creating an object of the Product class using the parameterized constructor which initializes the values of productId, productName, and price. The category attribute is not initialized, so it will not be displayed.
Solution 2
The code snippets provided are written in Java language. Here are the expected outputs for each of them:
-
Product p = new Product(); p.display();Output:0 null 0.0Explanation: This is because you are creating a new Product object with the default constructor, which doesn't initialize any of the attributes. In Java, the default values for int, float, and char are 0, 0.0, and '\u0000' respectively, and for objects (like String), it's null.
-
Product p = new Product(71,"Mobile",80000,'A'); p.display();Output:71 Mobile 80000.0 AExplanation: Here, you are creating a new Product object with the parameterized constructor that takes four arguments. The values of the attributes are set to the values passed in the constructor.
-
Product p = new Product(501,"Wallet",520); p.display();Output:501 Wallet 520.0Explanation: In this case, you are creating a new Product object with the parameterized constructor that takes three arguments. The values of the attributes are set to the values passed in the constructor. The category attribute is not initialized, so it takes the default value for char, which is '\u0000' and it's not printed.
Similar Questions
Analyze the below program, and fill the correct code so that it produces the below output: 0 101class Book { private int bookId; private double bookPrice; public int getBookId() { return bookId; } public void setBookId(int bookId) { this.bookId = bookId; } public double getBookPrice() { return bookPrice; } public void setBookPrice(double bookPrice) { this.bookPrice = bookPrice; }}public class Test { public static void main(String a[]) { Book bobj=new Book(); blank blank blank }}Note : Same drag option can be used multiple times. Analyse and use the correct optionSystem.out.println(bobj.getBookPrice()); bobj.setBookId(101); System.out.println(bobj.getBookId());
public class Employee {private int id;private String name;private float salary;private String pancardno;public Employee(){System.out.println("In default constructor");}public Employee(int id, String name, float salary) {this.id = id;this.name = name;this.salary = salary;}public Employee(int id, String name, float salary, String pancardno) {this(id,name,salary); this.pancardno = pancardno; }this();}
Problem StatementImplement a product inventory system for an e-commerce application. Your task is to create a Product class to represent individual products. The Product class should include the following attributes: id (integer): A unique identifier for each product.name (string): The name of the product.price (double): The price of the product.quantity (integer): The quantity available in stock.Your goal is to overload the = operator to enable the assignment of one Product object to another. When you assign one Product to another, it should copy all the attributes, creating a deep copy of the object.Input format :The first line of input consists of an integer representing the product ID.The second line of input consists of a string representing the product name.The third line of input consists of a double value representing the product price.The fourth line of input consists of an integer value representing the product quantity.Output format :The program displays the following information:The details of the original product(Product 1 details), including ID, name, price, and quantity in separate lines.The details of the copied product (Product 2 details), including ID, name, price, and quantity in separate lines.Refer to the sample outputs for the formatting specifications.Code constraints :Price is printed as such without any specific precisions.Sample test cases :Input 1 :1Widget A10.9950Output 1 :Product 1 details:ID: 1Name: Widget APrice: $10.99Quantity: 50Product 2 details (copy):ID: 1Name: Widget APrice: $10.99Quantity: 50Input 2 :102Dell253.65Output 2 :Product 1 details:ID: 102Name: DellPrice: $253.6Quantity: 5Product 2 details (copy):ID: 102Name: DellPrice: $253.6Quantity: 5
Given the following set of classes:public class Customer { private String userName; private String password; public Customer(String userName, String password) { this.userName = userName; this.password = password; } public void setPassword(String password) { this.password = password; } public String getPassword() { return password; } public void setUserName(String userName) { this.userName = userName; } public String getUserName() { return userName; }} class CustomerTest { public static void main(String[] args) { Customer testAcct = new Customer("user","pass"); testAcct.setPassword("Java"); System.out.println(testAcct.toString()); }}What does the toString() method need to look like to result in the following output:UserName: userPassword: Javaa.)public String toString() { String state = "UserName: " + userName + "\n"; state += "Password: " + password + "\n"; return state;}b.)public String toString() { String state = "UserName: " + userName + "\n"; state += "Password: " + password + "\n"; return password;}c.)public String toString() { String state = "UserName: " + userName + "\n"; state = "Password: " + password + "\n"; return state;}d.)public String toString() { String state = "username: " + userName + "\n"; state += "password: " + password + "\n"; return state;}
Given1. public class Certification{2. private String name;3. private double price;4. 5. public Certification(String s,double d){6. name = s;7. price = d;8. }9. 10. public double getPrice(){11. return price;12. }13. 14. public String getName(){15. return name;16. }17. }18. 19. //Assume necessary imports have done20. 21. public class CertifyTest {22. 23. public static void main(String[] args) {24. 25. List<Certification> certs = new ArrayList<Certification>();26. 27. certs.add(new Certification("1Z0-803",120));28. certs.add(new Certification("1Z0-804",250));29. certs.add(new Certification("1Z0-805",175));30. certs.add(new Certification("1Z0-808",150));31. certs.add(new Certification("1Z0-810",225));32. 33. //insert here34. 35. for(Certification c : certs){36. if(filter.test(c)){37. System.out.println(c.getName());38. }39. }40. }41. 42. }Suppose we have to print only the certifications where price are higher than 180, which of the following insert at line 33 will achieve that?*Predicate<Certification> filter = (c) -> return c.getPrice() > 180;Predicate <Certification> filter = (c) -> {return c.getPrice() > 180;};Consumer <Certification> filter = () -> {return c.getPrice() > 180;};Consumer <Certification> filter = (c) -> {return c.getPrice() > 180;};Supplier <Certification> filter = (c) -> {return c.getPrice() > 180;};
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.