Objectives
Make the transition to object oriented
programming.
Go over some of the basics concepts:
Classes
Instance variables
Methods
Object state
The keyword "this", what it does
toString method
Introduction
Phase 1
Review the following Java programs as an example of a
Java class and client code that uses
it: MimicOct.java and UnderTheSea.java.
All of you have been working with classes in Java. One
of the most important things to know about classes is that they are
actually blueprints for objects. They basically contain all of the
information as to how objects are built.
One of the most important features of objects is the
fact that they have state and behavior. The way that they hold
state is through instance variables. Instance variables are simply
variables that "describe" the object and its state. There are
several ways to handle the accessibility of an instance variable
within an object, but in this lab we are going to work only with
public instance variables. Public means that any class within the
project that you're working in has permission to access and modify
the instance variables. To access or modify an instance variable
that is public, such as populationNumber, in an object called
species, simply type species.populationNumber =
someInteger.
Methods are the ways that objects perform their
behavior. Our focus will be on non-static methods; these require an
instance of the class in order to operate. Static methods are
methods that don't need an instance of
an object to call it.
Phase 2
Please download Species.java and CircleOfLife.java.
Use CircleOfLife.java for testing Species.java as you follow these
steps:
Phase 3
Create a Species constructor that takes in a String for its name, an int for its population, and an int for its growth rate. Don't let someone initialize a species with a population above 1500 or below 1, or a growth rate outside of the range of 1 to 20 percent. If the population is initialized below 1, set it to 1, and if above 1500, set it to 1500. Similarly for the growth rate.
Phase 4
Return an appropriate String in the toString method
that describes the state of a Species object.
Example String to return in the toString() method:
Name of species: cat
Population: 1200
Growth Rate: 12%
Once you have created a toString method, you can use
it to display a Species object stored in a variable s as
follows:
System.out.println(s);
which is a shortcut for
System.out.println(s.toString());
Phase 5
Create two species objects of whatever animal that you wish (do this in CircleOfLife.java).
Phase 6
Complete the code for mergeSpecies(Species other). This is a non-static method (as all of the rest of them are) that takes another species as a parameter. This method adds the populations of the two species, changes the name of the species to the concatenation of the two names, and the growth rate to the maximum of the two growth rates
Phase 7
Complete the code for grow() and populationInXYears(int x) (hint: don't change the value of the population for the populationInXYears method -- simply return what the population will be in X years given the growth rate of that species.
EXAMPLE
If the species are rabbits and their growth rate is 10%, population is 100, and x is 2, you should see
The projected population for the rabbits in 2 years will be 121.
The updated source code is mentioned below with the inline definitions of previously incomplete functions.
Species.java
public class Species {
// Put the instance variable here.
String name;
int population;
int growth_rate;
// Create a Species constructor that takes in
a String for its name, an int for
// its population, and an int for its growth
rate per year as a percent.
public Species(String name, int population,
int growth)
{
this.name = name;
if (population < 1)
population = 1;
if (population > 1500
) population = 1500;
this.population =
population;
if (growth < 1)
growth = 1;
if (growth > 20)
growth = 20;
this.growth_rate =
growth;
}
// mergeSpecies adds the populations of the two
species, changes the name
// of the species to the concatenation of the
two names, and the growth
// rate to the maximum of the two growth
rates.
public void mergeSpecies(Species other)
{
// Adding two
populations in both the species instances
this.population +=
other.population;
other.population =
this.population;
// changing name of
both the species
this.name += " ";
this.name +=
other.name;
other.name =
this.name;
// changing both the
growth rates to max of growth rates
if (this.growth_rate
< other.growth_rate)
this.growth_rate = other.growth_rate;
other.growth_rate =
this.growth_rate;
//System.out.println("mergeSpecies NOT IMPLEMENTED YET");
}
public String toString(){
String output = "Name of
Species: ";
output +=
this.name;
output += "\n";
output += "Population:
";
output +=
this.population;
output += "\n";
output += "Growth Rate:
";
output +=
this.growth_rate;
output += "\n";
//System.out.println("toString NOT IMPLEMENTED YET");
return output;
}
// Increases the population according to the
growth rate of the species, i.e.
// updates the population instance variable by
adding to it the growth rate/100 times the current population.
public void grow() {
population +=
(growth_rate * population)/100;
//System.out.println("grow NOT IMPLEMENTED YET");
}
// Returns the population of the species in x years according to its growth rate.
public int populationInXYears(int x){
//System.out.println("getPoplulationInXYears NOT IMPLEMENTED
YET");
int local =
population;
while (x > 0)
{
local += (growth_rate* local )/100;
x--;
}
return local;
}
public static void main(String[] args) {
// Put simple code
here to test the Species class. It is always a good idea to
include
// a main method in any
class you define.
Species a = new
Species("Frog", 100, 10);
System.out.println(a);
a.grow();
System.out.println(a);
System.out.println("a.populationInXYears(10) returns " + a.populationInXYears(10));
Species b = new
Species("Rabbit", 10, 23);
a.mergeSpecies(b);
System.out.println("a.mergeSpecies(b) is " + a);
}
}
CircleOfLife.java
public class CircleOfLife {
public static void main(String args[]) {
// Create a new
Species object here, passing in the appropriate arguments
Species a = new
Species("Rabbit", 100, 10);
// Print out the
species' growth rate
System.out.println("Growth rate of species Rabbit " +
a.growth_rate);
// Use the species'
toString here
System.out.println(a);
// Call
populationInXYears here
System.out.println("Population of Rabbit species in 2 years " +
a.populationInXYears(2));
// Create a new Species
object here, passing in the appropriate arguments
// using a very large
number for the population (e.g. 100000000)
Species b = new
Species("Dog", 1000500, 12);
// Print out the
species' population to make sure it is set to 1500
System.out.println("Population of Dog initialized : " +
b.population);
// Call
populationInXYears here, feel free to hardcode in the int to be
passed to the method
System.out.println("Dog
population in 3 years " + b.populationInXYears(3));
// Call mergeSpecies
here. Test that mergeSpecies is doing what you expected it to
a.mergeSpecies(b);
System.out.println("After merging total population : " +
b.population);
}
}
Objectives Make the transition to object oriented programming. Go over some of the basics concepts: Classes...
Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...
Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and name variables (do not include topTrick). Note: The type refers to what the breed typically does; for example, a corgi would be a “cattle herding dog.” A Shiba Inu would be a “hunting dog.” Create the setTopTrick() mutator method Dog is parent class Corgi and Driver are subclasses Complete the Corgi class: Using the UML Class diagram, declare the instance variables. Create the two...
Java is an object-oriented programming language that enables us to define classes and to instantiate them into objects. These objects then call each other’s methods to implement the behavior of the application. The Unified Modeling Language (UML) is an object-oriented visual notation to document the design of object-oriented classes. For this discussion, you will practice designing a Java class called Course, drawing a UML class diagram for the Course class, and then implementing the Course class in Java code. Review...
JAVA :The following are descriptions of classes that you will create. Think of these as service providers. They provide a service to who ever want to use them. You will also write a TestClass with a main() method that fully exercises the classes that you create. To exercise a class, make some instances of the class, and call the methods of the class using these objects. Paste in the output from the test program that demonstrates your classes’ functionality. Testing...
I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java Programming Project #4 – Classes and Objects (20 Points) You will create 3 new classes for this project, two will be chosen from the list below and one will be an entirely new class you invent.Here is the list: Shirt Shoe Wine Book Song Bicycle VideoGame Plant Car FootBall Boat Computer WebSite Movie Beer Pants TVShow MotorCycle Design First Create three (3) UML diagrams...
According to the following information, define Account, CheckingAccount, and TestPart1 classes. 1. Account and CheckingAccount classes Account - number: int - openDate: String - name: String - balance: double + Account(int, String, String, double) + deposit(double): void + withdraw (double): boolean + transferTo(Account, double): int + toString(): String CheckingAccount + CheckingAccount(int, String, String, double) + transferTo(Account, double): int Given the above UML diagam, define Account and CheckingAccount classes as follow: Account (8 points) • public Account(int nu, String op, String...
Computer Science 111 Introduction to Algorithms and Programming: Java Programming Net Beans Project #4 - Classes and Objects (15 Points) You will create 3 new classes for this project, two will be chosen (THE TWO CHOSEN ARE TENNIS SHOE AND TREE) from the list below and one will be an entirely new class you invent. Here is the list: Cellphone Clothes JuiceDrink Book MusicBand Bike GameConsole Tree Automobile Baseball MusicPlayer Laptop TennisShoe Cartoon EnergyDrink TabletComputer RealityShow HalloweenCostume Design First Create...
Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person and Student represent individuals with a first and last name as String class variables, and Student has an additional int class variable that represents a ID number. The Roster class represents a group of people that are objects of either the Person or Student class. It contains an ArrayList. The Person class has been completed for you with class variables, a constructor, and a...
Page 1/2 ECE25100 Object Oriented Programming Lab 8: Inheritance Description: The purpose of this lab is to practice inheritance. To get credit for the lab, you need to demonstrate to the student helper that you have completed all the requirements. Question 1: Consider the following detailed inheritance hierarchy diagram in Fig. 1. 1) The Person.java constructor has two String parameters, a first name and a last name. The constructor initializes the email address to the first letter of the first...
This is the question about object-oriend programming(java) please show the detail comment and prefect code of each class, Thank you! This question contain 7 parts(questions) Question 1 Create a class a class Cat with the following UML diagram: (the "-" means private , "+" means public) +-----------------------------------+ | Cat | +-----------------------------------+ | - name: String | | - weight: double | +-----------------------------------+ | + Cat(String name, double weight) | | + getName(): String | | + getWeight(): double | |...