I spent a few hours Friday night helping some friends who are going back to school to get their engineering degrees. Before the semester started, they told me how excited they were that they got to do start their first programming class in Java. Now, I have been dealing with Visual Basic and C# for the past 6-7 years, to include Classic ASP, DHTML (JavaScripts), Flash 8 Action Scripts and Cold Fusion. Never really took a gander at Java but was surprised to see how similar it was to C#.
Since I lived around the corner, I rode over there to see what they were up to and come to find out all I saw on the faces was confusion and frustration. I laughed and told them that they didn't want any advice from a “Self Taught Hacker. “ One had said, “At this point, I would take advice from my little daughter. “ So after looking over what they have done, I had to explain to them about the classes, constructors, private properties, public variables and methods. I pretty much tried to put it in perspective as best as I could. End result, one got frustrated and the other, I noticed a little light upstairs come on. Did I mention it took 3 hours for me to help them with their project? If they read this, I don't want them to take this personal. I wrote their project in C# and expanded a little more on it, I think it took me all but 15 minutes or so and another 10 for tweaking the console.
This part of the code serves really no purpose, but to give them a better example:
using System;
public class Student
{
#region Student Constructor
/// <summary>
/// Constructor
/// </summary>
/// <param name="StudentName">Student Name as String</param>
public Student(String StudentName)
{
this.studentname = StudentName;
}
#endregion Student Constructor
#region Properties
/// <summary>
/// Properties of Student's name
/// </summary>
private String studentname;
public String StudentName
{
get { return studentname; }
set { studentname = value; }
}
/// <summary>
/// Properties of the Total combined quiz score
/// </summary>
private Int32 totalquizscore = 0;
public Int32 TotalQuizScore
{
get { return totalquizscore; }
set { totalquizscore = value; }
}
/// <summary>
/// Properties of one quiz score
/// </summary>
private Int32 quizscore = 0;
public Int32 QuizScore
{
get { return quizscore; }
set { quizscore = value; }
}
/// <summary>
/// Counter to total quizzes inputed
/// </summary>
private Int32 quizcount = 0;
public Int32 QuizCount
{
get { return quizcount; }
set { quizcount = value; }
}
/// <summary>
/// Gets the Average Quiz Grade from the Total Quiz Score and Quiz Count
/// </summary>
public Double QuizAverage
{
get { return totalquizscore / quizcount; }
}
/// <summary>
/// Stores an array of multiple quiz scores
/// </summary>
private Int32[] quizscorearray;
public Int32[] QuizScoreArray
{
get { return quizscorearray; }
set { quizscorearray = value; }
}
#endregion Properties
#region Methods
/// <summary>
/// Gets the student's name
/// </summary>
/// <returns></returns>
public String getStudentName()
{
return studentname;
}
/// <summary>
/// Adds one quiz score
/// </summary>
/// <param name="QuizScore">One Quiz score as Integer</param>
public void addQuizScore(Int32 QuizScore)
{
//Increments the quizcount by 1
this.quizcount += 1;
//Sets QuizScore variable by the input parameter value
this.QuizScore = QuizScore;
//Sums the Total Quiz Score by the QuizScore variable
this.totalquizscore += this.quizscore;
}
/// <summary>
/// Adds the array of quiz scores, uses the addQuizScore method
/// </summary>
/// <param name="QuizScores">Integer Array of Quiz scores</param>
public void addQuizScores(Int32[] QuizScores)
{
//Sets the QuizScoreArray variable to the input parameter value
this.QuizScoreArray = QuizScores;
//Loops through each QuizScores Array
for (int i = 0; i < QuizScores.Length; i++)
{
//Add the score by using the addQuizScore method
addQuizScore(QuizScores[i]);
}
}
#endregion Methods
}
class Program
{
static void Main(string[] args)
{
//Creates a new instance of the object Student
Student StudentReport = new Student("Zonka");
//Adds quiz grades
StudentReport.addQuizGrade(90);
StudentReport.addQuizGrade(85);
StudentReport.addQuizGrade(50);
StudentReport.addQuizGrade(100);
StudentReport.addQuizGrade(10);
//Outputs using local methods
OuputStudentName(StudentReport);
OutputQuizAverage(StudentReport);
//Separater from the first object example
Console.WriteLine("\n\n\n\n\n\n\n\n");
//Sets a new instance of the object Student setting a new student name
StudentReport = new Student("Clifford");
//Creates a local array of quiz scores
Int32[] CliffQuizzes = { 76, 84, 78, 98, 68 };
//Uses the addQuizGrades passing the array of grades
StudentReport.addQuizGrades(CliffQuizzes);
//Outputs using local methods
OuputStudentName(StudentReport);
OutputQuizGrades(StudentReport);
OutputQuizAverage(StudentReport);
}
/// <summary>
/// Outputs the students name in a console format
/// </summary>
/// <param name="StudentReport">StudentReport object as Student</param>
private static void OuputStudentName(Student StudentReport)
{
Console.WriteLine("====================================");
Console.WriteLine(String.Format("Student name: {0}", StudentReport.StudentName));
}
/// <summary>
/// Outputs the student's score for each quiz in a console format
/// </summary>
/// <param name="StudentReport">StudentReport object as Student</param>
private static void OutputQuizGrades(Student StudentReport)
{
Console.WriteLine("------------------------------------");
Console.WriteLine("Quiz\t\t\tScore");
Console.WriteLine("------------------------------------");
for (int i = 0; i < StudentReport.QuizScoreArray.Length; i++)
{
Console.WriteLine(String.Format("Chapter {0} Quiz\t\t{1}", i + 1, StudentReport.QuizScoreArray[i]));
}
Console.WriteLine("------------------------------------");
Console.WriteLine(String.Format("Total\t\t\t{0}\n", StudentReport.TotalQuizScore));
}
/// <summary>
/// Outputs the student's Quiz count and Average Quiz Grade in a console format
/// </summary>
/// <param name="StudentReport">StudentReport object as Student</param>
private static void OutputQuizAverage(Student StudentReport)
{
Console.WriteLine("------------------------------------");
Console.WriteLine(String.Format("Total Quizzes:\t\t{0}", StudentReport.QuizCount));
Console.WriteLine(String.Format("Average Grade:\t\t{0}", StudentReport.QuizAverage));
Console.WriteLine("====================================");
}
}
Posted
Jan 30 2006, 11:40 PM
by
LarryZonka