Java help
week6_src/AbstractShape.java
week6_src/AbstractShape.java
package
edu
.
drexel
.
ct290
;
public
abstract
class
AbstractShape
implements
Shape
{
@
Override
public
double
area
()
{
// TODO Auto-generated method stub
return
0
;
}
}
__MACOSX/week6_src/._AbstractShape.java
week6_src/assignment/GradeBook.java
week6_src/assignment/GradeBook.java
package
edu
.
drexel
.
ct290
.
assignment
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Scanner
;
public
class
GradeBook
{
private
String
course
;
private
ArrayList
<
Person
>
students
=
new
ArrayList
<
Person
>
();
private
ArrayList
<
GradeBookEntry
>
entries
=
new
ArrayList
<
GradeBookEntry
>
();
public
GradeBook
(
String
course
){
this
.
course
=
course
;
}
public
String
getCourse
()
{
return
course
;
}
public
void
setCourse
(
String
course
)
{
this
.
course
=
course
;
}
public
void
addStudent
(
Person
student
){
students
.
add
(
student
);
}
public
void
addEntry
(){
System
.
out
.
println
(
"Grade which student: "
);
for
(
int
i
=
0
;
i
<
students
.
size
();
i
++
)
{
System
.
out
.
println
(
i
+
" "
+
students
.
get
(
i
).
getName
());
}
Scanner
reader
=
new
Scanner
(
System
.
in
);
int
studentIndex
=
reader
.
nextInt
();
reader
.
nextLine
();
System
.
out
.
println
(
"Enter the assessment name: "
);
String
assessmentName
=
reader
.
nextLine
();
System
.
out
.
println
(
"Homework (1) or exam (2): "
);
int
entryType
=
reader
.
nextInt
();
reader
.
nextLine
();
GradeBookEntry
newEntry
;
// TODO: create either a Homework or Exam entry
// TODO: add getData method to the Homework and Exam
newEntry
.
getData
();
newEntry
.
setStudent
(
students
.
get
(
studentIndex
));
entries
.
add
(
newEntry
);
}
public
void
listGrades
(){
for
(
int
i
=
0
;
i
<
entries
.
size
();
i
++
){
GradeBookEntry
entry
=
entries
.
get
(
i
);
entry
.
printEntry
();
// This could also be condensed to one line:
// entries.get(i).printEntry();
}
}
public
void
displaySummary
(){
// TODO: show a distribution of grades in this class.
// See the barchart example in Java HTP 7.4
// Initialize the chart
String
[]
distribution
=
new
String
[
5
];
distribution
[
0
]
=
"F: "
;
distribution
[
1
]
=
"D: "
;
distribution
[
2
]
=
"C: "
;
distribution
[
3
]
=
"B: "
;
distribution
[
4
]
=
"A: "
;
for
(
GradeBookEntry
entry
:
entries
){
if
(
entry
.
getLetterGrade
().
equals
(
"F"
)){
distribution
[
0
]
+=
"*"
;
}
if
(
entry
.
getLetterGrade
().
equals
(
"D"
)){
distribution
[
1
]
+=
"*"
;
}
if
(
entry
.
getLetterGrade
().
equals
(
"C"
)){
distribution
[
2
]
+=
"*"
;
}
if
(
entry
.
getLetterGrade
().
equals
(
"B"
)){
distribution
[
3
]
+=
"*"
;
}
if
(
entry
.
getLetterGrade
().
equals
(
"A"
)){
distribution
[
4
]
+=
"*"
;
}
}
for
(
int
i
=
0
;
i
<
distribution
.
length
;
i
++
){
System
.
out
.
println
(
distribution
[
i
]);
}
}
}
__MACOSX/week6_src/assignment/._GradeBook.java
week6_src/assignment/GradeBookEntry.java
week6_src/assignment/GradeBookEntry.java
package
edu
.
drexel
.
ct290
.
assignment
;
import
java
.
util
.
Scanner
;
public
class
GradeBookEntry
{
private
Person
student
;
private
String
assessmentName
;
private
int
numericGrade
;
public
GradeBookEntry
(
String
assessmentName
){
this
.
assessmentName
=
assessmentName
;
}
public
GradeBookEntry
(
Person
student
,
String
assessmentName
)
{
this
.
student
=
student
;
this
.
assessmentName
=
assessmentName
;
}
public
GradeBookEntry
()
{
}
// The next six methods are just getters and setters
// for the member variables of this class.
public
Person
getStudent
()
{
return
student
;
}
public
void
setStudent
(
Person
person
)
{
this
.
student
=
person
;
}
public
String
getAssessmentName
()
{
return
assessmentName
;
}
public
int
getNumericGrade
(){
return
this
.
numericGrade
;
}
public
void
setAssessmentName
(
String
assessmentName
)
{
this
.
assessmentName
=
assessmentName
;
}
public
String
getLetterGrade
(){
GradeConverter
converter
=
new
GradeConverter
();
return
converter
.
convertGrade
(
this
.
getNumericGrade
());
}
public
void
printEntry
(){
System
.
out
.
println
(
"Assignment: "
+
getAssessmentName
()
);
System
.
out
.
println
(
"For student: "
+
getStudent
().
getName
()
);
System
.
out
.
println
(
"Grade: "
+
getNumericGrade
()
);
System
.
out
.
println
(
"Letter Grade: "
+
getLetterGrade
()
);
}
}
__MACOSX/week6_src/assignment/._GradeBookEntry.java
week6_src/assignment/GradeBookTest.java
week6_src/assignment/GradeBookTest.java
package
edu
.
drexel
.
ct290
.
assignment
;
import
java
.
util
.
Scanner
;
public
class
GradeBookTest
{
public
static
void
main
(
String
[]
args
)
{
GradeBook
book
=
new
GradeBook
(
"TestCourse"
);
Person
person1
=
new
Person
(
"John"
);
Person
person2
=
new
Person
(
"Lisa"
);
Person
person3
=
new
Person
(
"Bill"
);
Person
person4
=
new
Person
(
"Sarah"
);
book
.
addStudent
(
person1
);
book
.
addStudent
(
person2
);
book
.
addStudent
(
person3
);
book
.
addStudent
(
person4
);
book
.
addEntry
();
book
.
addEntry
();
book
.
addEntry
();
book
.
listGrades
();
book
.
displaySummary
();
}
}
__MACOSX/week6_src/assignment/._GradeBookTest.java
week6_src/assignment/GradeConverter.java
week6_src/assignment/GradeConverter.java
package
edu
.
drexel
.
ct290
.
assignment
;
import
java
.
util
.
Scanner
;
public
class
GradeConverter
{
public
String
convertGrade
(
int
numberGrade
){
if
(
numberGrade
>=
90
){
return
"A"
;
}
else
if
(
numberGrade
>=
80
)
{
return
"B"
;
}
else
if
(
numberGrade
>=
70
){
return
"C"
;
}
else
if
(
numberGrade
>=
60
){
return
"D"
;
}
else
{
return
"F"
;
}
}
/**
* This method gets input from the user
*
@return
a grade in number format from 0-100
*/
public
int
getNumberGrade
(){
// declare and intialize variables
int
userInput
=
0
;
Scanner
reader
=
new
Scanner
(
System
.
in
);
// get the user input
System
.
out
.
print
(
"Enter the number grade: "
);
userInput
=
reader
.
nextInt
();
// return the input to the caller of this method
return
userInput
;
}
/**
*
@param
args
*/
public
static
void
main
(
String
[]
args
)
{
GradeConverter
converter
=
new
GradeConverter
();
int
input
=
converter
.
getNumberGrade
();
String
letterGrade
=
converter
.
convertGrade
(
input
);
System
.
out
.
println
(
"The letter grade for "
+
input
+
" is "
+
letterGrade
);
}
}
__MACOSX/week6_src/assignment/._GradeConverter.java
week6_src/assignment/Person.java
week6_src/assignment/Person.java
package
edu
.
drexel
.
ct290
.
assignment
;
import
java
.
util
.
Scanner
;
public
class
Person
{
private
String
name
;
private
int
age
;
private
String
email
;
private
String
ssn
;
public
Person
(){
}
// A simple constrctor to initialize the name
// NOTE: constructors do not have return types and
// are named after the class name.
public
Person
(
String
name
){
// Keyword this distinguishes between the member of this class,
// and the parameter name;
this
.
name
=
name
;
// the rest of the member variables will have default values
}
// This constructor initializes everything
public
Person
(
String
name
,
int
age
,
String
email
,
String
ssn
){
this
.
name
=
name
;
this
.
age
=
age
;
this
.
email
=
email
;
this
.
ssn
=
ssn
;
}
// This is a copy constructor. It takes an object of the same
// class, and copies all the member variables over.
// Then you will have two separate objects with identical data
public
Person
(
Person
person
){
this
.
name
=
person
.
name
;
this
.
age
=
person
.
age
;
this
.
email
=
person
.
email
;
this
.
ssn
=
person
.
ssn
;
}
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
String
getEmail
()
{
return
email
;
}
public
void
setEmail
(
String
email
)
{
this
.
email
=
email
;
}
public
void
getData
(){
Scanner
reader
=
new
Scanner
(
System
.
in
);
System
.
out
.
print
(
"Enter the person's name: "
);
name
=
reader
.
nextLine
();
System
.
out
.
print
(
"Enter the person's age: "
);
age
=
reader
.
nextInt
();
reader
.
nextLine
();
System
.
out
.
print
(
"Enter the person's email: "
);
email
=
reader
.
nextLine
();
System
.
out
.
println
(
"Enter SSN: "
);
ssn
=
reader
.
nextLine
();
}
public
String
getId
(){
return
ssn
;
}
public
String
toString
(){
return
"Name: "
+
name
+
"\nAge: "
+
age
+
"\nemail: "
+
email
;
}
}
__MACOSX/week6_src/assignment/._Person.java
week6_src/assignment/Student.java
week6_src/assignment/Student.java
package
edu
.
drexel
.
ct290
.
assignment
;
import
java
.
util
.
Scanner
;
import
edu
.
drexel
.
ct290
.
Person
;
public
class
Student
extends
Person
{
private
String
studentId
;
private
String
gradeLevel
;
// A constructor for student:
public
Student
(
String
name
,
String
Id
,
String
gradeLevel
){
// you can reuse the superclass constructors:
super
(
name
);
// then add initialization for Student specific variables:
this
.
studentId
=
Id
;
this
.
gradeLevel
=
gradeLevel
;
}
@
Override
public
String
toString
(){
// Get the person string from the super class:
String
studentString
=
super
.
toString
();
// TODO: Add the student specific info:
studentString
+=
"\nId: "
+
studentId
+
"\nYear: "
+
gradeLevel
;
return
studentString
;
}
@
Override
public
void
getData
(){
super
.
getData
();
Scanner
reader
=
new
Scanner
(
System
.
in
);
System
.
out
.
print
(
"Enter student id: "
);
studentId
=
reader
.
nextLine
();
System
.
out
.
print
(
"Enter grade level: "
);
gradeLevel
=
reader
.
nextLine
();
}
}
__MACOSX/week6_src/assignment/._Student.java
__MACOSX/week6_src/._assignment
week6_src/Circle.java
week6_src/Circle.java
package
edu
.
drexel
.
ct290
;
public
class
Circle
implements
Shape
{
private
double
radius
;
public
double
getRadius
()
{
return
radius
;
}
public
void
setRadius
(
double
radius
)
{
// keyword this distinguishes between the member variable
// radius of this class, and the method parameter named radius
this
.
radius
=
radius
;
}
@
Override
public
double
area
()
{
// Java's Math class has many helpful math methods and constants
// Math.pow raises a base to the given power
return
Math
.
PI
*
Math
.
pow
(
radius
,
2
);
}
// Notice that classes can have additional methods too.
public
double
diameter
(){
return
2
*
radius
;
}
}
__MACOSX/week6_src/._Circle.java
week6_src/ExceptionDemo.java
week6_src/ExceptionDemo.java
package
edu
.
drexel
.
ct290
;
import
java
.
util
.
InputMismatchException
;
import
java
.
util
.
Scanner
;
public
class
ExceptionDemo
{
private
Scanner
reader
;
// default constructor that initializes reader
public
ExceptionDemo
(){
reader
=
new
Scanner
(
System
.
in
);
}
public
void
notCatchingException
(){
System
.
out
.
print
(
"Enter a double: "
);
double
myDouble
=
reader
.
nextDouble
();
System
.
out
.
println
(
"You entered: "
+
myDouble
);
}
public
void
catchingException
(){
System
.
out
.
print
(
"Enter a double: "
);
double
myDouble
=
0
;
try
{
myDouble
=
reader
.
nextDouble
();
}
catch
(
Exception
e
){
System
.
out
.
println
(
"That's not a double: "
+
e
.
toString
());
return
;
}
System
.
out
.
println
(
"You entered: "
+
myDouble
);
}
public
void
ensureGoodData
(){
double
myDouble
=
0
;
boolean
successful
=
false
;
while
(
!
successful
){
try
{
System
.
out
.
println
(
"Enter a double: "
);
myDouble
=
reader
.
nextDouble
();
successful
=
true
;
}
catch
(
InputMismatchException
e
)
{
System
.
out
.
println
(
"That's not a double!"
);
successful
=
false
;
reader
.
nextLine
();
}
}
System
.
out
.
println
(
"You entered: "
+
myDouble
);
}
public
double
divide
(
long
a
,
long
b
){
double
answer
=
0
;
try
{
answer
=
a
/
b
;
}
catch
(
Exception
e
){
System
.
out
.
println
(
"Sorry, division by zero is undefined: "
+
e
.
getMessage
());
}
return
answer
;
}
public
void
dontHandleException
()
throws
InputMismatchException
{
System
.
out
.
print
(
"Enter a double: "
);
double
myDouble
=
0
;
try
{
myDouble
=
reader
.
nextDouble
();
}
catch
(
InputMismatchException
e
){
throw
e
;
}
System
.
out
.
println
(
"You entered: "
+
myDouble
);
}
public
void
tryCatchFinally
(){
System
.
out
.
print
(
"Enter a double: "
);
double
myDouble
=
0
;
try
{
myDouble
=
reader
.
nextDouble
();
}
catch
(
Exception
e
){
System
.
out
.
println
(
"That's not a double: "
+
e
.
toString
());
return
;
}
finally
{
System
.
out
.
println
(
"At least I tried!"
);
}
System
.
out
.
println
(
"You entered: "
+
myDouble
);
}
public
static
void
main
(
String
[]
args
)
{
ExceptionDemo
demo
=
new
ExceptionDemo
();
demo
.
notCatchingException
();
//demo.catchingException();
//demo.ensureGoodData();
//double answer = demo.divide(42, 0);
// Up to this method to handle the exception
//demo.dontHandleException();
//demo.tryCatchFinally();
}
}
__MACOSX/week6_src/._ExceptionDemo.java
week6_src/gradebook.docx
ASSIGNMENT - GRADEBOOKENTRY
Make GradeBookEntry an abstract class. Remove numericGrade member variable.
Make getNumericGrade an abstract method. Add getData as an abstract method.
Make two subclasses of GradeBookEntry - HomeworkEntry and ExamEntry.
Exam has a curve. getNumericGrade should add the curve to the numericGrade.
HomeworkEntry can just return numericGrade as is.
Add appropriate constructors.
Add appropriate exception handling.
Base code is in the assignment folder inside src.
·
·
__MACOSX/week6_src/._gradebook.docx
week6_src/Person.java
week6_src/Person.java
package
edu
.
drexel
.
ct290
;
import
java
.
util
.
Scanner
;
public
class
Person
{
private
String
name
;
private
int
age
;
private
String
email
;
private
String
ssn
;
// A default constructor. If no constructors were declared
// then the JVM would provide a default that did nothing.
// If another constructor is declared, then you must also
// declare a default constructor if you want one. If you do not,
// you will not be able to instantiate an object without any parameters.
public
Person
(){
name
=
""
;
age
=
0
;
email
=
""
;
ssn
=
""
;
}
// A simple constructor to initialize the name
// NOTE: constructors do not have return types and
// are named after the class name.
public
Person
(
String
name
){
// Keyword 'this' distinguishes between the member of this class,
// and the parameter name;
this
.
name
=
name
;
// the rest of the member variables will have default values
}
// This constructor initializes everything
public
Person
(
String
name
,
int
age
,
String
email
,
String
ssn
){
this
.
name
=
name
;
this
.
age
=
age
;
this
.
email
=
email
;
this
.
ssn
=
ssn
;
}
// This is a copy constructor. It takes an object of the same
// class, and copies all the member variables over.
// Then you will have two separate objects with identical data
public
Person
(
Person
person
){
this
.
name
=
person
.
name
;
this
.
age
=
person
.
age
;
this
.
email
=
person
.
email
;
this
.
ssn
=
person
.
ssn
;
}
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
String
getEmail
()
{
return
email
;
}
public
void
setEmail
(
String
email
)
{
this
.
email
=
email
;
}
public
void
getData
(){
Scanner
reader
=
new
Scanner
(
System
.
in
);
System
.
out
.
print
(
"Enter the person's name: "
);
name
=
reader
.
nextLine
();
System
.
out
.
print
(
"Enter the person's age: "
);
age
=
reader
.
nextInt
();
reader
.
nextLine
();
System
.
out
.
print
(
"Enter the person's email: "
);
email
=
reader
.
nextLine
();
System
.
out
.
println
(
"Enter SSN: "
);
ssn
=
reader
.
nextLine
();
}
public
String
getId
(){
return
ssn
;
}
public
String
toString
(){
return
"Name: "
+
name
+
"\nAge: "
+
age
+
"\nemail: "
+
email
;
}
}
__MACOSX/week6_src/._Person.java
week6_src/Rectangle.java
week6_src/Rectangle.java
package
edu
.
drexel
.
ct290
;
public
class
Rectangle
implements
Shape
{
private
long
width
;
private
long
height
;
public
long
getWidth
()
{
return
width
;
}
public
void
setWidth
(
long
width
)
{
// keyword this distinguishes between the member variable
// radius of this class, and the method parameter named radius
this
.
width
=
width
;
}
public
long
getHeight
()
{
return
height
;
}
public
void
setHeight
(
long
height
)
{
this
.
height
=
height
;
}
@
Override
public
double
area
()
{
return
height
*
width
;
}
}
__MACOSX/week6_src/._Rectangle.java
week6_src/Shape.java
week6_src/Shape.java
package
edu
.
drexel
.
ct290
;
public
interface
Shape
{
public
double
area
();
}
__MACOSX/week6_src/._Shape.java
week6_src/ShapeTest.java
week6_src/ShapeTest.java
package
edu
.
drexel
.
ct290
;
public
class
ShapeTest
{
public
static
void
main
(
String
[]
args
)
{
// Create an array of shapes.
// Notice that the interface is used as the type
Shape
[]
shapes
=
new
Shape
[
2
];
Circle
circle
=
new
Circle
();
circle
.
setRadius
(
4
);
Rectangle
rec
=
new
Rectangle
();
rec
.
setWidth
(
4
);
rec
.
setHeight
(
6
);
// Assign the specific shapes to the array
// Since they implement Shape, they can be used as objects of
// type Shape in the code.
shapes
[
0
]
=
circle
;
shapes
[
1
]
=
rec
;
for
(
int
i
=
0
;
i
<
shapes
.
length
;
i
++
){
// Print out the area using only the method defined in Shape
System
.
out
.
println
(
"The area is: "
+
shapes
[
i
].
area
()
);
}
}
}
__MACOSX/week6_src/._ShapeTest.java
week6_src/Student.java
week6_src/Student.java
package
edu
.
drexel
.
ct290
;
import
java
.
util
.
Scanner
;
import
edu
.
drexel
.
ct290
.
Person
;
public
class
Student
extends
Person
{
private
String
studentId
;
private
String
gradeLevel
;
// A constructor for student:
public
Student
(
String
name
,
String
Id
,
String
gradeLevel
){
// you can reuse the superclass constructors:
super
(
name
);
// then add initialization for Student specific variables:
this
.
studentId
=
Id
;
this
.
gradeLevel
=
gradeLevel
;
}
@
Override
public
String
toString
(){
// Get the person string from the super class:
String
studentString
=
super
.
toString
();
// TODO: Add the student specific info:
studentString
+=
"\nId: "
+
studentId
+
"\nYear: "
+
gradeLevel
;
return
studentString
;
}
@
Override
public
void
getData
(){
super
.
getData
();
Scanner
reader
=
new
Scanner
(
System
.
in
);
System
.
out
.
print
(
"Enter student id: "
);
studentId
=
reader
.
nextLine
();
System
.
out
.
print
(
"Enter grade level: "
);
gradeLevel
=
reader
.
nextLine
();
}
}