Question & Answer: 1) Create a boat class with at least two properties. Write methods to archive the objects of boat type…..

1) Create a boat class with at least two properties. Write methods to archive the objects of boat type to disk and then reads and print the attributes.

Expert Answer

 

import java.util.Scanner;

//Boat class

public class Boat

{

//3 properties of boat

int length;

int weight;

int cost;

//method to read properties into objects

public void read()

{

Scanner input =new Scanner(System.in);

System.out.println(“Enter Length:”);

length=input.nextInt();

System.out.println(“Enter Weight:”);

weight=input.nextInt();

System.out.println(“Enter Cost:”);

cost=input.nextInt();

}

//method to print the object’s attributes

public void print()

{

System.out.println(“nLength: “+length);

System.out.println(“nWeigth: “+weight);

System.out.println(“nCost: “+cost);

}

//main method

public static void main(String args[])

{

Boat b1=new Boat();//b1 is object for boat

Boat b2=new Boat();//b2 is object for boat

//calling read method to read inputs from user

//for boat b1

b1.read();

//for boat b2

b2.read();

//calling print method to printing attributes of boat b1

b1.print();

System.out.println(“———————“);

//calling print method to print attributes of boat b2

b2.print();

}

}

Still stressed from student homework?
Get quality assistance from academic writers!