1) Create a class representing a Book with instance variables for title, author, and price. Include methods to display book details and set the price. Create an object of the class and demonstrate its use.
package mypack;
import java.util.Scanner;
public class Array {
private String title;
private String author;
private Float price;
public Array(String title,String author,Float price){
this.title=title;
this.price =price;
this.author=author;
}
public void display(){
System.out.println(" The title is " +title);
System.out.println("The author name is " +author);
System.out.println("The price of book is " +price);
}
public void setPrice(Float price){
this.price = price;
}
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
System.out.println("Enter title");
String title = S.next();
System.out.println("Enter Author name");
String author = S.next();
System.out.println("Enter Price");
Float price = S.nextFloat();
Array array = new Array(title,author,price);
System.out.println("Book Details");
array.display();
System.out.println("Enter Updated Price");
Float pric = S.nextFloat();
array.setPrice(pric);
System.out.println("After setting new Price");
array.display();
}
}
package mypack;
public class ConstructorOverloading{
private String model = "Default Model";
private int year = 2012;
public ConstructorOverloading( String model){
this.model = model;
}
public ConstructorOverloading(int year){
this.year = year;
}
public void display(){
System.out.println("Model " +model);
System.out.println("Year " +year);
}
public static void main(String [] args){
ConstructorOverloading C1 = new ConstructorOverloading("Samsung 11");
ConstructorOverloading C2 = new ConstructorOverloading(2011);
C1.display();
C2.display();
}
}
No comments:
Post a Comment