Objectives - Continue To Practice To Write The Code For The Data Type Class
Objectives - Continue To Practice To Write The Code For The Data Type Class
LAB #6
OBJECTIVES
-continue to practice to write the code for the data type class
-Learn how to write the code and access the overloaded methods
-Learn how to write the code and access the static methods and the static data members
-Understand relationship between the data type class and the controlling class
PART 1
public class TheClass
{
// A ? where A includes three following lines
private String str;
private static int intNum = ;
private double doubleNum ;
// ! ?
public TheClass"#
{
str = $un%nown&;
doubleNum = ';
(
// C ?
public TheClass "String s) double d#
{
str = s;
doubleNum = d;
(
// * ?
public static void static+ethod " #
{
intNum ,,;
(
// - ?
public void setStr "String s#
{
str = s;
(
// . ?
public int get/ntNum"#
{
return intNum;
(
// 0 ?
public String toString"#
{
1eturn $String = $ , str , $2n/nteger Number = $ , intNum;
(
(
3uestion45
6rovide the name or brief description of the above session or methods
A' 7ere $str& and $doubleNum& are the non8static instance variables of
$TheClass& and $intNum& is a static instance variable of $TheClass& with
default value $&'
!' This is a default or non8parameteri9ed constructor of $TheClass& which
initiali9es the two non8static instance variables $str& and $doubleNum&
with default values $un%nown& and $'& respectivel:'
C' This is a parameteri9ed constructor of $TheClass& which initiali9es the two
non8static variables with the values passed to it'
*' This is a static method of $TheClass& which increments the static instance
variable $intNum&'
-' This is a non8static method of $TheClass& which sets the value of the
instance variable $str& of the ob;ect on which this method is called'
.' This is a non8static method which returns the value of static instance
variable $intNum&'
0' This is a overridden method of $<b;ect& class) which when called upon an
ob;ect of $TheClass& will return a String ob;ect representing the value of
$str& and $intNum&'
3uestion=5
Assume a program containing the controlling class with the main method that
declares two TheClass ob;ects as follows5
public class ControllingClass
{
public static void main "String> ? args #
{
TheClass one = new TheClass"#;
// 75 calling the no8argument constructor
TheClass two = new TheClass"$*avid @illiam&)A'B#;
// /5 calling the parameter constructor to pass5 $*avid @illiam& and A'B
to the ob;ect
S:stem'out'println" one #; // C?
S:stem'out'println" two #; // D?
// call the method static+ethod
EEEEEEEEEEEEEEEEEEEE; // F ?
(
(
@rite the complete lines 7 and /
3uestionA5
@hat is the output at the line C and D?
.or line C
88888888888888888
String = un%nown
/nteger Number =
.or line D
888888888888888888
String = *avid @illiam
/nteger Number =
3uestionG5
@rite the code on line F
TheClass'static+ethod"#;
6A1T=5 <verloaded methods
@rite a class named OverloadedMethod_Pena that has three overloaded
methods named displayInformation5
accepts two int numbers then displa: them
accepts one int number and a String then displa: them
accepts two Strings then displa: them
1ecall5 <verloaded methods are methods that have the same name) but diHerent
signature'
public class <verloaded+ethodE:ourFastName
{
public void displa:/nformation"int num4) int num=#
{
S:stem'out'println"I/nteger 4 = I,num4,I2n/nteger = =
I,num=#;
(
public void displa:/nformation"int num) String str#
{
S:stem'out'println"I/nteger = I,num,I2nString = I,str#;
(
public void displa:/nformation"String str4) String str=#
{
S:stem'out'println"IString 4 = I,str4,I2nString = = I,str=#;
(
(
6A1T A5 @or% on Static methods
*esign a GeometryStatic_yourLastame class that onl: contains the following
static methods
A static method areaOf!ircle that accepts the radius of a circle and
returns the area of the circle'
Jse the following formula5
Area = Kr
=
Jse +ath'6/ for K and the radius of the circle for r where radius r is
entered from the %e:board in main"#
A static method named areaOfRectan"le that accepts the length and
width of a rectangle and returns the area of the rectangle where length
and width are entered from the %e:board in mainl"#'
Jse the following formula5
Area = Fength L @idth
A static method named areaOfS#uare that accepts the side of the
sMuare and return the area of the sMuare where the side is entered from
the %e:board in main"#
Jse the following formula5
Area = side N side
A static method named areaOfTrian"le that accepts the length of a
triangleOs base and the triangleOs height' The method should return the
area of the triangle where base and height are entered from the %e:board
in main"#
Jse the following formula5
Area = !ase L 7eight L 'B
Notes5 These methods should displa: an error message if negative values are
used for the circleOs radius) the rectangleOs length or width or the triangleOs
base or height'
NeLt) write a program that is named Area!alculation_yourlastame to test the
above methods) which displa:s the following menu and response to the userOs
selection5
0eometr: Calculator
4' Calculate the Area of Circle
=' Calculate the Area of a 1ectangle
A' Calculate the Area of a SMuare
G' Calculate the Area of a Triangle
B' 3uit'
-nter :our choice "48B#5 EEE
8/f the user selects a tas% from 4 to G; displa: the messages to as% users for the
data that need to calculate the area of the selected shape' Then) use the static
methods that are dePned in the above class 0eometr:StaticE:ourFastName to
calculate the area of the selected shape
*ispla: the result as following format "eLample below for a circle#
888888888888888888888888888888888888
C/1CF-
1adius = A'
The area is5 =Q'=R
888888888888888888888888888888888888
8After processing a tas% from 4 to G) displa: the above menu again to allow user to
continue using the application'
8/f user selects tas%B) terminate the program
$O% TO GRA&'
PART1
3uestion45 A!C*-.0 A'B
3uestion=5 7 / 4
3uestionA5 C D 4
3uestion G5 F 4
PART(
Name5 <verloaded+ethodE:ourFastName
+ethod accepts two integer numbers =
+ethod accepts one integer number and one string =
+ethod accepts two strings =
PART)
Name5 0eometr:StaticESourFastName '=B
+ethod are<fCircle5 name) static) formula) return value 4'B
+ethod area<f1ectangle5 name) static) formula) return
value
4'B
+ethod area<fSMuare5 name) static) formula) return
value
4'B
+ethod area<fTriangle5 name) static) formula) return
value
4'B
Name5 AreaCalculationE:ourlastName '=B
+enu and handle the loop to allows user to continue
after one tas%
=
7andle tas%s for each selection T call static methods A
<utput with correct format =
Compile success T MualiPed the reMuirement A
Comments 4
Total scores A
$O% TO T*R I
Submit the following Ples on eCampus
6art45 labRE:ourFastName'docL // include part4 and part=
6art=5 class 0eometr:E:ourFastName';ava) class AreaCalculationE:ourlastName';ava