Gregory Desrosiers Montreal

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

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
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.
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • The Quebec Ministerial Exam of College English (English Exit Exam)
    Hello, everyone. How you doing? I'm good. There is some unfortunate news for me to deliver, though I am planning to try to work myself a...
  • Computer Game Collection
    Hello, people! Before I happen to leave this very foundation of Champlain College Saint-Lambert (that's where I'm learning to get wh...
  • String Letter Counter (JAVA Program)
    Hello, guys. This is another JAVA Program I want to share with you guys. It's a counter for a specific letter in an entered string strai...
  • DOS Games to Play
    Hello, people. If there's one thing that I want to do when I have lots of money, it's to play full version DOS Games on my Windows P...
  • JAVA Program: Command-based String Remover in Text Files
    Hello, everyone. Let me share with you the code on one of the programs I did back in March 2013, while doing my Data Structures class. This ...
  • The Black Glove [Short Poem by me]
    This is a poem written by me back in April 2010. I'm not sure if this was for an assignment, but this is what I got. It may not be good ...
  • Fundraising Project (To Autism Society Canada)
    Hi, everyone! How are you people doing? I am feeling tired and stuff, but there are a couple of things I want to bring up with you. Tomorrow...
  • Best VGM Series
    Hello, guys. I want to present to you a series of YouTube imports of video game music, with some of them already recorded but in a form that...
  • Princess Peach In India [Story Idea]
    Here's a really fancy story I have made, although it is more of an initial idea and comes from a visual I had more than two years ago. E...
  • Super Mario Script 1 (v 2.3) [Super Mario Galaxy font, with a few photos]
    Hello there. Do you recognize the font of this text? I'll give you two clues. The one below is the a sample sentence of different sizes....

Blog Archive

  • ▼  2013 (38)
    • ►  August (2)
    • ►  July (9)
    • ▼  June (12)
      • "The Asperger Computer" outline and Spreading the ...
      • JAVA Program - Essay Class (From Chapter 11 of Ton...
      • JAVA Program: Circle Cannon Shooter (Shooting Game...
      • Message from my College English teacher
      • How to Play a DOS Game using DOSBox
      • JAVA Program: Finding Smallest Number (Demonstrati...
      • JAVA Program: Entering Non-Duplicate Numbers to a ...
      • Need some help on publicity with my e-book
      • JAVA Program: The PlayStation Class [SonyPlaystati...
      • The Asperger Computer - Second Edition Preview: Th...
      • Am I Happy on My Brother's 21st Birthday?
      • JAVA Program: The Nintendo GameCube Game Class [In...
    • ►  May (12)
    • ►  April (2)
    • ►  January (1)
  • ►  2012 (24)
    • ►  December (13)
    • ►  November (3)
    • ►  October (5)
    • ►  September (1)
    • ►  August (2)
Powered by Blogger.

About Me

Unknown
View my complete profile