Question set 1 (Java) object oriented:
1.Write a class called Student. The class should be able to store information regarding the name, age, gpa, and phone number of a Student object. Please write all the setter and getter methods. Finally, write a Demo class to demonstrate the use of the class by creating two different objects.
2.Use the same Student class from the previous problem. This time,you will write a different Demo class, in which you will create three Student objects. You will store these three objects in a Student array that can hold up to 10 Student objects. Write another method in the Demo class called show Students(). This method, when called from the main method, will show all the student information, one object at a time, in the array on the console.
3.Write a class called Cat. The class should have two variables to store data regarding the name (String type) and weight (double type) of each Cat object. All cat objects will have a weight of 15 ounces when born (upon creation). Write the getter and setter methods. In addition, write a method called feed(int foodAmount). The feed method will allow the user to enter the amount of food fed to the cat object. The weight of the cat will increase by 1 ounce for 10 ounces of food is fed to the cat.Now write a Demo class, in which you will create a Cat object. Give the cat a name and feed the cat 5 times by calling the feed method five times in a row. Each time the food amount fed is a random integer number between 5 to 15 ounces. Display the name and final weight in pounds and ounces of the cat.
4.Write a Book class that contains book title and book price information. Write a Demo class in which you will create 1000 books. Each book title is made of a String composed of 5 to 10 random uppercase letters. Each book price is made of a random double value ranging from 0.00 to 99.99. Print these books in a nice formatted table of three columns on the console. The first column contains serial numbers from 1 to 1000. The second column contains book titles, and the third column contains their matching book prices with two decimal places.
Answer 1) //Basic Student Class along with getters and setters
which will store the name, age, gpa, and phone number of a Student
object
public class Student {
//declaration of variables
private String name;
private int age;
private float gpa;
private long phoneNumber;
//Getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getGpa() {
return gpa;
}
public void setGpa(float gpa) {
this.gpa = gpa;
}
public double getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(long phoneNumber)
{
this.phoneNumber =
phoneNumber;
}
//Constructor
public Student() {
}
}
//Demo class to demonstrate two different objects for class
Student
public class Demo {
public static void main(String[] args) {
//s1 is the first object of class
Student
Student s1 = new Student();
s1.setName("Student 1");
s1.setAge(20);
s1.setGpa(9);
s1.setPhoneNumber(9876543210l);
//s2 is the second object of class
Student
Student s2 = new Student();
s2.setName("Student 2");
s2.setAge(22);
s2.setGpa(9);
s2.setPhoneNumber(9875643210l);
}
}
Answer 2) Using the same Student class from the previous
problem
I have created 3 different objevts s1, s2 and s3 in another Demo
class
public class Demo {
public static void main(String[] args) {
showStudents();
//This method will display details
all the student information, one object at a time
//I am demonstarting with object s1
}
private static void showStudents() {
//studentArray will upto 10 Student
objects
Student[] studentArray = new
Student[10];
//s1 is the
first object of class Student
Student s1 = new
Student();
s1.setName("Student 1");
s1.setAge(20);
s1.setGpa(9);
s1.setPhoneNumber(9876543210l);
studentArray[0]=s1;
//s2 is the
second object of class Student
Student s2 = new
Student();
s2.setName("Student 2");
s2.setAge(22);
s2.setGpa(9);
s2.setPhoneNumber(9875643210l);
studentArray[1]=s2;
//s3 is the
third object of class Student
Student s3 = new
Student();
s3.setName("Student 2");
s3.setAge(22);
s3.setGpa(9);
s3.setPhoneNumber(9875643210l);
studentArray[2]=s2;
System.out.println("Student(s1) name is
"+studentArray[0].getName());
System.out.println("Student(s1) age is
"+studentArray[0].getAge());
System.out.println("Student(s1) gpa is
"+studentArray[0].getGpa());
System.out.println("Student(s1) phone number is
"+studentArray[0].getPhoneNumber());
}
}
Answer 3) Basic cat class with getters and setters
public class Cat {
private String name;
private double weight = 15;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
//feed method
public void feed(double foodAmount) {
this.weight += foodAmount/10;
}
public Cat() {
}
}
//Demo class which will feed cat five times
public class DemoCat
{
public static void main(String[] args)
{
int min = 5, max = 15;
Cat catObject = new Cat();
catObject.setName("Lucy");
catObject.setWeight(15);
for (int i=1 ; i<=5; i++)
{
catObject.feed((Math.random() * ((max - min) + 1)) + min);
}
System.out.println("The name of the
cat is: "+catObject.getName()+"\n"+"The weight of the cat in ounces
is: "+catObject.getWeight()+ "and in pounds
"+catObject.getWeight()/16);
}
}
Answer 4) Basic book class with getters and setters
public class Book {
private String bookTitle;
private float bookPrice;
public String getBookTitle() {
return bookTitle;
}
public void setBookTitle(String bookTitle) {
this.bookTitle = bookTitle;
}
public float getBookPrice() {
return bookPrice;
}
public void setBookPrice(float bookPrice) {
this.bookPrice = bookPrice;
}
}
//Demo class for book
import java.util.ArrayList;
import java.util.List;
public class DemoBooks {
public static void main(String[] args) {
List<Book> bookList = new
ArrayList<Book>(1000);
for(int i=0; i<1000; i++)
{
Book b = new Book();
b.setBookPrice(1000);
b.setBookTitle("Book1");
bookList.add(b);
System.out.println(b.getBookPrice());
System.out.println(b.getBookTitle());
}
}
}
Question set 1 (Java) object oriented: 1.Write a class called Student. The class should be able...
cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher, price, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Write another class called Bookshelf, which has name and array of Book objects. Bookself capacity is maximum of five books. Includes method for Bookself that adds, removes,...
7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food to represent a Lunch food item. Fields include name, calories, carbs In a main method (of a different class), ask the user "How many things are you going to eat for lunch?". Loop through each food item and prompt the user to enter the name, calories, and grams of carbs for that food item. Create a Food object with this data, and store each...
Write a Java object class Cat with the member variables (fields) color, breed, birthday, and weight. Create an empty constructor and overload that constructor and have it accept values for each member variable. There should be manipulator (setter) and accessor (getter) methods for each member variable. Ensure the Cat object has a toString() method. The accessor method for the birthday should return the date in the format MM/DD/YYYY. Add an accessor method getAgeInYears() that calculates how many years old the...
in java Write a class called Flight that represents an airline flight. (50 pts) It should contain instance data that represents the airline name, flight number, departure city, destination cities, and flight price. Define the Flight constructor to accept and initialize all instance data. Overload the Flight constructor such that the initial value of instance data airline name by Delta. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the...
In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....
JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you created in the previous lab. In this lab, create a Student class with the following class variable: Student name: Name (Note: Name is a datatype) Student Id: String Create the getter and setter methods. In addition to these methods, create a toString() method, which overrides the object class toString() method. This override method prints the current value of any of this class object. Complete...
Write a python program using Object Oriented and do the following: 1. create class "cat" with the following properties: name, age (create constructor method: def __init__) 2. create class "adopter" with the following properties: name, phone 3. create class "transaction" with these properties: adopter, cat (above objects) cat1 = "puffy, 2" adopter1 = "Joe, 123" Test your program: Joe adopts puffy. Print: "Per Transaction 1 <joe> has adopted <puffy>" this can only be done with object oriented programming, no way...
( Object array + input) Write a Java program to meet the following requirements: 1. Define a class called Student which contains: 1.1 data fields: a. An integer data field contains student id b. Two String data fields named firstname and lastname c. A String data field contains student’s email address 1.2 methods: a. A no-arg constructor that will create a default student object. b. A constructor that creates a student with the specified student id, firstname, lastname and email_address...
in JAVA Which of the following attributes would be MOST appropriate for a class called Book, that represents a book in a library? libraryName, location, libraryHours title, author, typeOfBook pages, cost, shipping anything, anytime, anywhere Which of the following statements is FALSE regarding constructors? A constructor allows you to set up an object once it is instantiated. A constructor does not have a return type. A constructor must have the same name as the name of the class. You can...
Create a class called AddressBook. It should be able to store a maximum of 50 names and 50 phone numbers (make an array of structures in the object or two arrays). It should contain a method called addName that will take a name and phone number and add it to the address book. There should be a method called findName that will take a string for the name and return the phone number if found in the list otherwise return...