Hello, everyone.
This is the first blog of a whole series of blogs I'm writing and publishing rapidly because what's going on is, I'm trying to build an audience for my e-book, The Asperger Computer, and yet still be able to finish the e-book between June 20 and 25th. I definitely want to start selling the book and make lots of money aside from getting myself a job, with the help of my parents and my dad's girlfriend. However, I haven't been really building an audience for the past few days, and still feel like I am totally new to the blogging community, so I'm going to work as fast as possible to get five or more blogs out.
Anyway, the first blog for today is the code for a custom-designed class I've worked on in my second JAVA course at college. What I had to do was to design a class called GradedActivity first, and then extend it to a subclass called Essay.
Here's the code for the GradedActivity class:
/**
A class that holds a grade for a graded activity.
*/
public class GradedActivity
{
private double score; // Numeric score
/**
The setScore method sets the score field.
@param s The value to store in score.
*/
public void setScore(double s)
{
score = s;
}
/**
The getScore method returns the score.
@return The value stored in the score field.
*/
public double getScore()
{
return score;
}
/**
The getGrade method returns a letter grade
determined from the score field.
@return The letter grade.
*/
public char getGrade()
{
char letterGrade;
if (score >= 90)
letterGrade = 'A';
else if (score >= 80)
letterGrade = 'B';
else if (score >= 70)
letterGrade = 'C';
else if (score >= 60)
letterGrade = 'D';
else
letterGrade = 'F';
return letterGrade;
}
}
There's no multiple constructors, but this is a demonstration of a mutable object where you can change the score by calling the "setScore" method.
Here's the code for the extended class, the Essay class:
public class Essay extends GradedActivity
{
private int grammar;
private int spelling;
private int correctLength;
private int content;
public Essay()
{
grammar = 0;
spelling = 0;
correctLength = 0;
content = 0;
}
public Essay(int gramScore, int spelScore, int cLScore, int contScore)
{
grammar = gramScore;
spelling = spelScore;
correctLength = cLScore;
content = contScore;
}
void setGrammar(int grammar)
{
this.grammar = grammar;
}
void setSpelling(int spelling)
{
this.spelling = spelling;
}
void setCorrectLength(int correctLength)
{
this.correctLength = correctLength;
}
void setContent(int content)
{
this.content = content;
}
int getGrammarScore()
{
return grammar;
}
int getSpellingScore()
{
return spelling;
}
int getCorrectLengthScore()
{
return correctLength;
}
int getContentScore()
{
return content;
}
int getTotal()
{
return grammar + spelling + correctLength + content;
}
public String toString()
{
return "Grammar : " + grammar + " pts.\nSpelling : " + spelling + " pts.\nLength : "
+ correctLength + " pts.\nContent : " + content + " pts.";
}
}
And here's a demonstration of the class:
public class EssayClass
{
public static void main(String[] args)
{
Essay englishEssayEmpty = new Essay();
Essay englishEssay = new Essay(30, 20, 20, 30);
englishEssayEmpty.setGrammar(20);
englishEssayEmpty.setSpelling(15);
englishEssayEmpty.setCorrectLength(5);
englishEssayEmpty.setContent(28);
englishEssayEmpty.setScore(englishEssayEmpty.getTotal());
englishEssay.setScore(englishEssay.getTotal());
System.out.println(englishEssayEmpty.getGrammarScore() + "\n" + englishEssayEmpty.getSpellingScore() +
"\n" + englishEssayEmpty.getCorrectLengthScore() + "\n"
+ englishEssayEmpty.getContentScore() + "\n" + englishEssayEmpty.getTotal() + "\n" +
englishEssayEmpty.getScore() + "\n" + englishEssayEmpty.getGrade() + "\n");
System.out.println(englishEssayEmpty + "\n\nScore : "+ englishEssayEmpty.getTotal() +" pts.\n\nGrade : "
+ englishEssayEmpty.getGrade() + "\n\n" + englishEssay + "\n\nScore : " +
englishEssay.getTotal() +" pts.\n\nGrade : " + englishEssay.getGrade());
}
}
You can take the class source code and experiment with new methods or cases. This is simply what I wanted to show off; just the source code for the Essay class.
Anyway, I'll see you later with a new, more attracting, blog.
Thursday 13 June 2013
JAVA Program - Essay Class (From Chapter 11 of Tony Gaddis' Starting out with JAVA, 4th Edition)
Posted on 07:54 by Unknown
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment