Complete Chapter 7 Programming Activity 1 Writing a Class De
Complete Chapter 7, Programming Activity 1: Writing a Class Definition, Part 1.
Make sure you study the Programming Activity 7-1 Guidance document
Need it in Java please.
Solution
So you need an example code that implements every aspect of class building:
The below-given code defines a book that contains four private data members of different types.
We shall use constuctors to initialize the variables
We have declared all the setters and getter methods as required.
See comments for clarification
class Book {
private String title;
private String author;
private int pages;
private double price;
Book() { //non parameterized constructor
title = author = null;
price = pages = 0;
}
Book(String title, String author, int pages, double price){ //parameterized constructor
this.title = title;
this.author = author;
this.pages = pages;
this.price = price;
}
//all setter methods are given below
void setTitle(String title){
this.title = title; //check the use of this keyword
}
void setAuthor(String newAuthor){
author = newAuthor; //when parameter name is different, this keyword is optional
}
void setPages(int pages){
this.pages = pages;
}
void setPrice(double newPrice){
this.price = newPrice; //when parameter name is different, this keyword is optional
}
//All getter methods are defined below
String getTitle(){
return title;
}
String getAuthor(){
return author;
}
int getPages(){
return pages;
}
double getPrice(){
return price;
}
}

