1. Based on the Person class in the Lecture Notes, define a subclass Teacher including one instance variable, office ( String type), and two methods, setOffice , and toString that returns the name,...

1 answer below »

1. Based on the
Person
class in the Lecture Notes, define a subclass
Teacher
including one instance variable,
office
(String
type), and two methods,
setOffice, and
toString
that returns the name, and office. Then write a driver that prompts the user to input a teacher's name and office information from the keyboard, and then output the teacher’ info to the monitor by invoking the
toString
method.






2. Based on the
Time
class and its derived class
ExtTime
class in the Lecture Notes, write a driver to test the polymorphism. That is, given the two objects of
Time
and
ExtTime
classes as below,





ExtTime x =
new
ExtTime();
Time y =
new
Time();
read the values of
x
and
y
from the keyboard, and then increase both objects by
two seconds
by invoking the
increment() method twice (also inherited to the subclass). Then pass the two objects through the same
display
method so to output the info of objects to monitor. Use a loop, so the process could run continuously.
Requirements: the time values should be read in the format of
hh:mm:ss.






3. Based on the
Line
class in the Lecture Notes, read the four data (x0, y0, x1, y1) for a
Line
object from the keyboard, and output its length to the monitor by calling the corresponding method. Use a loop, so the process could be run continuously.







Page 1 of 18 Module 6 Object-Oriented Programming Contents Module 6 Object-Oriented Programming.....................................................................................1 OOP Inheritance ......................................................................................................................2 Using the toString Method .......................................................................................................5 Section Exercises .....................................................................................................................8 More Examples: Point and Pixel Classes ............................................................................... 10 OOP Composition ................................................................................................................. 11 Section Exercises ................................................................................................................... 13 Polymorphism ....................................................................................................................... 15 Chapter Exercises .................................................................................................................. 17 Page 2 of 18 Java is one of the most popular Object-Oriented Programming (OOP) languages. The traditional (C, Pascal, Fortran) program is a function-based or structured program that consists of some functions: fun1 ( …) .. fun2 ( …) .. An object-oriented program (OOP) consists of objects: obj1 … obj2 … OOP Inheritance With a base class or super class, we can define its derived class or subclass that includes inherited methods and new methods. A super-class is declared as: public class Time { private int hrs; private int mins; private int secs; public Time() // default constructor { hrs = 0; Page 3 of 18 mins = 0; secs = 0; } public Time(int h, int m, int s) { hrs = h; mins = m; secs = s; } public void set(int h, int m, int s) { hrs = h; mins = m; secs = s; } public void writeOut() { if (hrs < 10)="" system.out.print("0");="" system.out.print(hrs="" +="" ":");="" if="" (mins="">< 10)="" system.out.print("0");="" system.out.print(mins="" +="" ":");="" if="" (="" secs="">< 10="" )="" system.out.print("0");="" system.out.print(secs);="" }="" public="" void="" increment="" ()="" {="" secs="" ++;="" if="" (secs=""> 59) { secs = 0; mins ++; if (mins > 59) { mins = 0; hrs ++; if (hrs > 23) hrs = 0; } } } public boolean equal (Time other) { return (hrs==other.hrs && mins==other.mins && secs==other.secs); } } A derived class or subclass named ExtTime includes the string zone. Then we can declare a subclass ExtTime derived from its super class Time shown as below. ALL of the variables and methods of the super class are inherited into its derived classes or called subclasses, but the methods of the subclass with the same name will overwrite that of the super class (also called the base class). The diagrams of the above classes are illustrated as below. Page 4 of 18 public class ExtTime extends Time { private String zone; public ExtTime () { super(); // invoking the default constructor of Time class zone = “EST”; } public ExtTime ( int h, int m, int s, String z) { super(h, m, s); // invoking the general constructor of Time zone = z; } public void set (int h, int m, int s, String z) { set(h, m, s); // invoking the set inherited from Time class zone = z; } // … set(int h, int m, int s); // inherited from Time class // … increment (); // inherited from Time class public void writeOut () // overwrite that of Time class { super.writeOut(); System.out.print(" " + zone); } public boolean equal (ExtTime other) // overwrite that of Time { return (super.equal(other) && zone==other.zone); } } Page 5 of 18 Note: The set method inherited from the Time class is different from the other set method since they have different parameters. The writeOut and equal methods of the ExtTime class will overwrite that of the Time class. In the constructors of the ExtTime class, the variables (hrs, mins, and secs) of the Time class can be initialized through the keyword, super, meaning its super class, Time class. Since the variables are declared as private in the Time class, they could NOT be directly accessed by any members of the ExtTime class. Example (a) Write the code to declare an object x of ExtTime initialized with 8:35:0 PST. ExtTime x = new ExtTime(8, 35, 0, “PST”); (c) Write the code to declare an object y of ExtTime initialized with 0:0:0 EST. ExtTime y = new ExtTime(); Using the toString Method The toString method is briefly introduced in the last module for Fraction Class. In Java, the toString method is defined in the base class. That is, it could be used in any of user-defined classes. It is very convenient to use since it can be omitted when it is passed as an argument in the standard output statement, System.out.println. An example is as follows.

Answered 2 days AfterAug 12, 2021

Answer To: 1. Based on the Person class in the Lecture Notes, define a subclass Teacher including one instance...

Shweta answered on Aug 14 2021
154 Votes
89522/Program1/build/classes/.netbeans_automatic_build
89522/Program1/build/classes/.netbeans_update_resources
89522/Program1/build/classes/program1/Person.class
package program1;
public synchronized class Person {
private String name;
public void Person();
public void Person(String);
public void setName(String);
public String getName();
public String toString();
public boolean equals(Person);
}
89522/Program1/build/classes/program1/Program1.class
package program1;
public synchronized class Program1 {
public void Program1();
public static void main(String[]);
}
89522/Program1/build/classes/program1/Teacher.class
package program1;
public synchronized class Teacher extends Person {
private String office;
public void Teacher();
public void Teacher(String, String);
public void setOffice(String);
public String toString();
}
89522/Program1/build.xml

Builds, tests, and runs the project Program1.


89522/Program1/manifest.mf
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
89522/Program1/nbproject/build-impl.xml















































































































































































































Must set src.dir
Must set test.src.dir
Must set build.dir
Must set dist.dir
Must set build.classes.dir
Must set dist.javadoc.dir
Must set build.test.classes.dir
Must set build.test.results.dir
Must set build.classes.excludes
Must set dist.jar




































































































Must set javac.includes
































































































































No tests executed.























































































































































































































































Must set JVM to use for profiling in profiler.info.jvm
Must set profiler agent JVM arguments in profiler.info.jvmargs.agent





























































































































































































































Must select some files in the IDE or set javac.includes













































To run this application from the command line without Ant, try:

java -jar "${dist.jar.resolved}"










































Must select one file in the IDE or set run.class



Must select one file in the IDE or set run.class






















Must select one file in the IDE or set debug.class




Must select one file in the IDE or set debug.class




Must set fix.includes









This target only works when run from inside the NetBeans IDE.








Must select one file in the IDE or set profile.class
This target only works when run from inside the NetBeans IDE.








This target only works when run from inside the NetBeans IDE.












This target only works when run from inside the NetBeans IDE.



































Must select one file in the IDE or set run.class





Must select some files in the IDE or set test.includes




Must select one file in the IDE or set run.class




Must select one file in the IDE or set applet.url






































































Must select some files in the IDE or set javac.includes



















Some tests failed; see details above.








Must select some files in the IDE or set test.includes



Some tests failed; see details above.



Must select some files in the IDE or set test.class
Must select some method in the IDE or set test.method



Some tests failed; see details above.




Must select one file in the IDE or set test.class



Must select one file in the IDE or set test.class
Must select some method in the IDE or set test.method













Must select one file in the IDE or set applet.url








Must select one file in the IDE or set applet.url




















































89522/Program1/nbproject/genfiles.properties
build.xml.data.CRC32=92235e72
build.xml.script.CRC32=17b5e2ab
[email protected]
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=92235e72
nbproject/build-impl.xml.script.CRC32=07a88219
nbproject/[email protected]
89522/Program1/nbproject/private/private.properties
compile.on.save=true
user.properties.file=C:\\Users\\Acer\\AppData\\Roaming\\NetBeans\\8.2\\build.properties
89522/Program1/nbproject/private/private.xml




file:/C:/Users/Acer/Documents/NetBeansProjects/Program1/src/program1/Person.java
file:/C:/Users/Acer/Documents/NetBeansProjects/Program1/src/program1/Program1.java
file:/C:/Users/Acer/Documents/NetBeansProjects/Program1/src/program1/Teacher.java


89522/Program1/nbproject/project.properties
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processor.options=
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
${run.classpath}
debug.test.classpath=\
${run.test.classpath}
# Files in build.classes.dir which should be excluded from distribution jar
dist.archive.excludes=
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/Program1.jar
dist.javadoc.dir=${dist.dir}/javadoc
excludes=
includes=**
jar.compress=false
javac.classpath=
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.external.vm=true
javac.processorpath=\
${javac.classpath}
javac.source=1.8
javac.target=1.8
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}
javac.test.processorpath=\
${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
main.class=program1.Program1
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
${javac.classpath}:\
${build.classes.dir}
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
source.encoding=UTF-8
src.dir=src
test.src.dir=test
89522/Program1/nbproject/project.xml

org.netbeans.modules.java.j2seproject


Program1







...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here