Question

In: Computer Science

summer/** * This Object-Oriented version of the "Summer" class * is a simple introduction to constructors...

summer/**
* This Object-Oriented version of the "Summer" class
* is a simple introduction to constructors /
* private data members / static vs. not static / and the
* "toString" method.
*
* SKELETON FOR LAB TEST.
*
* @author Raymond Lister
* @version April 2015;
*/
public class SummerOO
{
public static int numSummers = 0;
// The above variable is used to count the number of
// instances of the class SummerOO that have been created.
// (i.e. the number of objects that have been created.)
//
// Note: "numSummers" is declared as "static". It is a
// variable that belongs to the class, not an
// individual object.
// See Parsons page 117,
// section "6.8 Static Fields and Methods"
// See https://docs.oracle.com/javase/tutorial/java/ ...
// ... javaOO/classvars.html
// Or google java tutorial static fields

/*
* The instance variables follow (i.e. not static). These
* are also called "fields? or ?private data members?.
* Each object has its own copy of these variables.
*/

public int sum; // sum of all integers received
public int count; // number of integers received

/**
* The constructors now follow. There are two constructors.
* Both constructors have the same name, since all
* constructors always have the same name as the class.
* The constructors are distinguishable because one of the
* constructors requires zero parameters while the other
* constructor requires a single integer parameter.
* See Parsons, page 105,
* section 6.3.1 "Overloaded Constructors"
*/

/**
* This is a ?dangerous? constructor, since the average is
* undefined when the object is created.
*
* This constructor and the method reset()
* are similar. The differences are that:
* (1) The constructor can only be used once, when the
* object is created.
* (2) The method reset() can't create an object, but it can
* be used whenever we like, as many times as we like,
* after the object has been created.
*/
public SummerOO()
{
sum = 0;
count = 0;
NumSummers = NumSummers;
}

/**
* This is a safer constructor, since the average is
* well defined when the object is created.
*
* This constructor and the method reset(int firstNumber)
* are similar. The differences are that:
* (1) The constructor can only be used once, when the
* object is created.
* (2) The method reset() can't create an object, but can
* be used whenever we like, as many times as we like,
* after the object has been created.
*
* @param firstNumber The first number of a series
*/
public SummerOO(int firstNumber)
{
sum = firstNumber;
count = 1;
NumSummers = NumSummers + 1;
}

/**
* Receives and processes a new number in the series.
*
* NOTE TO STUDENTS: When studying this code, experiment in
* BlueJ, by adding "static" into "public void putNumber".
* When you compile, you'll get an error message ...
*
* "non-static variable sum cannot be referenced from a
* static context".
*
* In other words a "static" method (which belongs to the
* class, not an individuaual object), can't access the
* variables in an object. This is for two reasons:
* (1) If we haven't created ANY objects yet, then there is
* no variable "sum" to access!
* (2) If multiple objects ("instances") of this
* class exist, then there are multiple versions of
* the "sum" variable, one version of "sum" in each
* object. The static method (which belongs to the
* class) cannot choose from the many versions of "sum".
* The same applies to the variable "count". The error
* message singled out "sum" because it occured before
* "count".
*
* @param newNumber a new number in the series
*/
public /* not static! */ void putNumber(int newNumber)
{
// This method is complete. No changes are required to
// "putNumber" in the lab test.
  
sum = sum + newNumber; // could write "sum += newNumber"
count = count + 1; // could write "++count"
}

/*
* A number of the methods from the "static" version
* have been left out of this object-oriented version.
* That's because Raymond did not wish to test you on
* those methods a second time, having already tested
* your knowledge of those methods in the "static"
* version.
*
* All those methods would appear in a complete
* version of this object-oriented version of the
* class, with the *ONLY* change being that the reserved
* word "static" would be deleted from the method
* header.
*
* The method putNumber() has been copied across to support
* the experiment of adding "static" to its header,
*/

/**
* Note that this is a static method.
*
* @return The number of objects that have been created.
*/
public static int getNumSummers()
{
return NumSummers;
}

/**
* It is common practise to supply a "toString" method
* in an object-oriented class. In fact, if you don't
* explicitly supply such a method, Java produces an
* implicit, simplistic "toString" method which produces
* a String like "SummerOO@1edd1f0". The word before
* the "@" is the name of the class. The hexadecimal
* number after the "@" is called the objects "hash code".
*
* Note: Method "toString" method is NOT "static". It
* can't be static, since the values in the data members
* may vary between objects of this class.
*
* See Nielsen, page 78,
* section "5.2.4 The toString Method"
* See Nielsen, page 165,
* section "8.2.1 Overriding the toString Method"
*
*@return The state of this "class instance" / "object"
*/
public string toString()
{
return "sum = " + sum + " count = " + count;
}

/**
* The purpose of this main method is to reinforce the
* lesson that anything that can be done through a BlueJ
* menu can also been done in some Java code.
*
* @param args Isn't used. Its here because PLATE always expects to see "main" methods which accepts as a parameter an array of Strings.
*/
public static void main(String [] args)
{
ClassName summer1 = new nameOfConstructor();
// the above line used the zero parameter constructor.

summer1.putNumber(17);
summer1.putNumber(1);

System.out.println(summer1.toString());
// in the above line, the ".toString()" can be omitted,
// to give just ...
// System.out.println(summer1);
//
// When the name of an object is given where a String
// is required (and println requires a String), Java
// automatically calls the "toString()" method of the
// object.

// Repeat for a second set of numbers
System.out.println();

ClassName summer2 = new nameOfConstructor(3);
// above line used the constructor that takes a parameter

summer2.putNumber(5);
summer2.putNumber(7);
System.out.println(summer2);
// in the above line, Java automatically calls the
// "toString()" method of the "summer2" object.

} // method main

} // class SummerOO

Solutions

Expert Solution

public class SummerOO

{

public static int NumSummers=0;//here changed "n" to "N" to get rid of errors

public int sum;

public int count;

public SummerOO()

{

sum=0;

count=0;

NumSummers=NumSummers;

}

public SummerOO(int firstNumber)

{

sum=firstNumber;

count=1;

NumSummers+=1;//can represent like this or NumSummers=NumSummers+1 both resembles same

}

public void putNumber(int newNumber)

{

sum+=newNumber;

count+=1;

}

public static int getNumSummers()

{

return NumSummers;

}

public String toString()//written String instead of string

{

return "sum = " + sum +"count = " +count;

}

public static void main(String[] args)

{

SummerOO summer1=new SummerOO();//classname and name of constructor both are SummerOO

summer1.putNumber(17);

summer1.putNumber(1);

System.out.println(summer1.toString());

System.out.println();

SummerOO summer2=new SummerOO(3);

summer2.putNumber(5);

summer2.putNumber(7);

System.out.println(summer2.to);

}

}

output:


Related Solutions

Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class...
Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class with public and private members and multiple constructors.  Gain a better understanding of the building and using of classes and objects.  Practice problem solving using OOP. Overview You will implement a date and day of week calculator for the SELECTED calendar year. The calculator repeatedly reads in three numbers from the standard input that are interpreted as month, day of month, days...
-What is object-oriented programming? -What is a class? -What is an object? -A contractor uses a...
-What is object-oriented programming? -What is a class? -What is an object? -A contractor uses a blueprint to build a set of identical houses. Are classes analogous to the blueprint or the houses? Explain. -What is a class diagram? How is it used in object-oriented programming? -What is an attribute in OOP? What is a data member? -What is a method in OOP? What is a member function? -What is the difference between private members and public members of a...
Code a simple Editor class with 2 constructors (one default, one with a string for a...
Code a simple Editor class with 2 constructors (one default, one with a string for a file name) The Editor class in private has an instance of a LinkedList (of strings) and the position of the user (use our Point class) create a short file - at least 5 lines - could be a program main: string fileName("input.txt"); Editor miniVi (fileName); miniVi.displayLines(); C++ explain in detail plz Thank you
What are the object oriented concepts and which all object oriented concepts are used in the...
What are the object oriented concepts and which all object oriented concepts are used in the given program? Consider the following code and explain how each of the object oriented concepts are applied in the given program. (CO1) class Vehicle { string brand; public: void honk(); void honk(int); }; void Vehicle::honk() { cout << "Tuut, tuut! \n" ; } void Vehicle::honk(int x) { for(int i=0;i<x;i++) cout << "Tuut, tuut! \n" ; } int main() { Vehicle V1; V1.honk(); V1.honk(3); }
Overloaded Constructors Add a pair of constructors to the Height class that implement the initializations provided...
Overloaded Constructors Add a pair of constructors to the Height class that implement the initializations provided by the two setHeight operations in Figure 7.11. Minimize the total number of statements by having the one parameter constructor call the one-parameter setHeight method and having the two-parameter constructor call the two-parameter setHeight method. Provide a complete rewritten main method for the HeightDriver class such that the new method uses one of the new constructors from part a) to generate this output: 6.0...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used to hold the rectangle’s width....
In this program, you are modifying given code so that the class is object-oriented. 2. Write...
In this program, you are modifying given code so that the class is object-oriented. 2. Write a Java class called CityDistancesOO in a class file called CityDistancesOO.java.    3. Your class will still make use of two text files. a. The first text file contains the names of cities with the first line of the file specifying how many city names are contained within the file.    b. The second text file contains the distances between the cities in the...
Write a Windows Form application named SumFiveInts. Microsoft Visual C#: An Introduction to Object-Oriented Programming,7th Edition....
Write a Windows Form application named SumFiveInts. Microsoft Visual C#: An Introduction to Object-Oriented Programming,7th Edition. Ch. 5, page 220. Take snip of program results.
object oriented programming java Create a Student class that have two data members id (assign to...
object oriented programming java Create a Student class that have two data members id (assign to your ID) and name (assign to your name). Create the object of the Student class by new keyword and printing the objects value. You may name your object as Student1. The output should be like this: 20170500 Asma Zubaida
Make a simple game using C++ which implements all about Object Oriented Programming (Please make an...
Make a simple game using C++ which implements all about Object Oriented Programming (Please make an explanation which of each part in it)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT