+1 (315) 557-6473 

Objects and classes assignment sample

Are you wondering where you can get objects and class assignment samples with a small budget? Get in touch with our objects and class assignment sample providers with just a few clicks of the mouse. We have been in the business of providing top-notch homework samples to students from all parts of the world on a 24/7 basis. Our objects and class homework samples are prepared by high-profile tutors drawn from the world's best universities. Hire us today and get the best objects and class homework samples.
Through the questions asked here, the examiner wants to test students' understanding of how to implement classes or the user defined data types. Examiner intends that students should comprehend the concept of encapsulation, accessor and mutator methods, implementation of constructors, and overload member methods. The intension is also to make students learn solve a programming problem with classes. In the first question examiner has asked to develop a class called RandomNumberGenerator. In the second question the examiner wants students to  develop a class called Book that represents a book, and another class called Author. In both the questions asked there are some extra constraints.

SOLUTION

Ex 1.

import java.util.Random;

/* class RandomNumberGenerator Implementation*/

class RandomNumberGenerator {

/* declare private member variable upperLimit and lowerLimit */

private int upperLimit;

private int lowerLimit;

/*

* define default constructor which assign value 6 to upperLimit and 1 to

* lowerLimit

*/

public RandomNumberGenerator() {

// TODO Auto-generated constructor stub

upperLimit = 6; /* upperLimit set to 6 */

lowerLimit = 1; /* lowerLimit set to 1 */

}

/*

* define parameterized constructor which assign value uLimit to upperLimit and

* lLimit to lowerLimit

*/

public RandomNumberGenerator(int uLimit, int lLimit) {

upperLimit = uLimit;/* upperLimit set to user given uLimit */

lowerLimit = lLimit;/* lowerLimit set to user given lLimit */

}

/* define getUpperLimit function return upperLimit */

public int getUpperLimit() {

return upperLimit;

}

/* define getLowerLimit function return lowerLimit */

public int getLowerLimit() {

return lowerLimit;

}

/* define setUpperLimit function set user given n value to upperLimit */

public void setUpperLimit(int n) {

upperLimit = n;

}

/* define setLowerLimit function set user given n value to setLowerLimit */

public void setLowerLimit(int n) {

lowerLimit = n;

}

/*

* define nextValueInt function which generate random between upperLimit

* ,lowerLimit

*/

public int nextValueInt() {

Random rn = new Random(); /* create Random class object rn */

int range = upperLimit - lowerLimit + 1; /* define range between upperLimit and lowerLimit */

return rn.nextInt(range) + lowerLimit; /* this will return generated random variable */

}

}

/* class Ex1 which have main function */

public class Ex1 {

/* define main function */

public static void main(String[] args) {

/* create RandomNumberGenerator class object obj */

RandomNumberGenerator obj = new RandomNumberGenerator();

/* show default value set in UpperLimit */

System.out.println(obj.getUpperLimit());

/* show default value set in LowerLimit */

System.out.println(obj.getLowerLimit());

obj.setLowerLimit(5); /* set LowerLimit member variable to 5 */

obj.setUpperLimit(56); /* set UpperLimit member variable to 56 */

/* show set value in UpperLimit */

System.out.println(obj.getUpperLimit());

/* show set value in LowerLimit */

System.out.println(obj.getLowerLimit());

/* show statement */

System.out.println("Play this combination - it will make you rich!");

/*

* created RandomNumberGenerator class object obj2 with parameterized

* constructor with UpperLimit=69,LowerLimit=1

*/

RandomNumberGenerator obj2 = new RandomNumberGenerator(69, 1);

/* define lotteryOutput array */

int lotteryOutput[] = new int[6];

/*

* generate 6 random number and assign to array and show these generated random

* variable

*/

for (int i = 0; i < 6; i++) {

/*

* generating random number from nextValueInt function from

* RandomNumberGenerator class function

*/

lotteryOutput[i] = obj2.nextValueInt();

/* show generating random number */

System.out.println(lotteryOutput[i]);

}

}

}

Ex 2.

/* define book class */

class Book {

/* declare  book private member variable */

private int numberOfPages;

private int yearOfPublication;

private double price;

private String title;

private Author author;

/* define Book class Default constructor which initialise member variable with given value in question*/

public Book() {

numberOfPages = 0;

yearOfPublication = 1900;

price = 0.0;

title = "";

author = null;

}

/* define Book class parameterized  constructor which initialise member variable with given value by user*/

public Book(int nPages, int yPub, double p, String t) {

numberOfPages = nPages;

yearOfPublication = yPub;

price = p;

title = t;

author = null;

}

/* define Book class copy constructor which copy value to  member variable from given Book object oriBook */

public Book(Book oriBook) {

numberOfPages = oriBook.numberOfPages;

yearOfPublication = oriBook.yearOfPublication;

price = oriBook.price;

title = oriBook.title;

author = oriBook.author;

}

/* define getNumberOfPages function which return numberOfPages from this object  */

public int getNumberOfPages() {

return numberOfPages;

}

/* define setNumberOfPages function which set numberOfPages in this object given by user  */

public void setNumberOfPages(int numberOfPages) {

this.numberOfPages = numberOfPages;

}

/* define getYearOfPublication function which return yearOfPublication from this object  */

public int getYearOfPublication() {

return yearOfPublication;

}

/* define setYearOfPublication function which set yearOfPublication in this object given by user  */

public void setYearOfPublication(int yearOfPublication) {

this.yearOfPublication = yearOfPublication;

}

/* define getPrice function which return price from this object  */

public double getPrice() {

return price;

}

/* define setPrice function which set price in this object given by user  */

public void setPrice(double price) {

this.price = price;

}

/* define getTitle function which return title from this object  */

public String getTitle() {

return title;

}

/* define setTitle function which set title in this object given by user  */

public void setTitle(String title) {

this.title = title;

}

/* define getAuthor function which return Author class object  from this object  */

public Author getAuthor() {

return author;

}

/* define setAuthor function which set given Author information to create new object of Auther class and return created object  */

public void setAuthor(String fName, String lName, int bYear, int nPub) {

author = new Author(fName, lName, bYear, nPub);

}

/* define toString function which show Book object information and Author object Information and return information

* of Book and Author in Sting format  */

public String toString() {

//create info string

String result = "Book Info: \nnumber of pages = " + Integer.toString(numberOfPages) + "\nyear of publication = "

+ Integer.toString(yearOfPublication) + "\nprice of book = " + Double.toString(price)

+ "\n title of book = " + title + "\nAuthor info:\n first name = " + author.getFirstName()

+ "\nlast name = " + author.getLastName() + "\n birth year = " + Integer.toString(author.getBirthYear())

+ "\n number of publications = " + Integer.toString(author.getNumberOfPublications());

System.out.println(result); //show string

return result;

}

}

/* Define Author class */

class Author {

/* Define Author class private member variable */

private String firstName;

private String lastName;

private int birthYear;

private int numberOfPublications;

/* Define Default constructor */

public Author() {

firstName = "";

lastName = "";

birthYear = 2018;

numberOfPublications = 0;

}

/* Define parameterized  constructor assign value to member variable given by user */

public Author(String fName, String lName, int bYear, int nPub) {

firstName = fName;

lastName = lName;

birthYear = bYear;

numberOfPublications = nPub;

}

/* Define copy constructor */

public Author(Author oriAuthor) {

firstName = oriAuthor.firstName;

lastName = oriAuthor.lastName;

birthYear = oriAuthor.birthYear;

numberOfPublications = oriAuthor.numberOfPublications;

}

/* Define getFirstName method which return firstName */

public String getFirstName() {

return firstName;

}

/* Define setFirstName method  which set firstName */

public void setFirstName(String firstName) {

this.firstName = firstName;

}

/* Define getLastName method which return lastName */

public String getLastName() {

return lastName;

}

/* Define setLastName method  which set lastName */

public void setLastName(String lastName) {

this.lastName = lastName;

}

/* Define getBirthYear method which return birthYear */

public int getBirthYear() {

return birthYear;

}

/* Define setBirthYear method  which set birthYear */

public void setBirthYear(int birthYear) {

this.birthYear = birthYear;

}

/* Define getNumberOfPublications method which return numberOfPublications */

public int getNumberOfPublications() {

return numberOfPublications;

}

/* Define setNumberOfPublications method  which set numberOfPublications */

public void setNumberOfPublications(int numberOfPublications) {

this.numberOfPublications = numberOfPublications;

}

}

/* Define Ex2 which has Main method */

public class Ex2 {

public static void main(String[] args) {

/* created book object using default constructor  */

Book book = new Book();

/* set number of pages = 50 to book object  */

book.setNumberOfPages(50);

/* set year of publication =2018  to book object  */

book.setYearOfPublication(2018);

/* set price =10000  to book object  */

book.setPrice(10000);

/* set set Title ="Round"  to book object  */

book.setTitle("Round");

/* set set Author information  to book object  */

book.setAuthor("Sumit", "goyal",1997, 50);

/*show Book and Author information in object book by toString method of book class*/

book.toString();

/* created book1 object using copy constructor in which copy book object to book1 object*/

Book  book1 = new Book(book);

/*show Book and Author information in object book1 by toString method of book class*/

book1.toString();

/*incrementing Number Of Publications object book1 by 1*/

book1.getAuthor().setNumberOfPublications(book1.getAuthor().getNumberOfPublications()+1);

/*show Book and Author information after update Number Of Publications by 1 in object book1 by toString method of book class */

book1.toString();

}

}