Jump to content


Check out our Community Blogs

Zer033x

Member Since 29 May 2008
Offline Last Active Jan 31 2015 09:32 AM
-----

#636494 Java:Tutorial Say Hello to Me and Say Hello to Java

Posted by Zer033x on 20 August 2012 - 07:38 PM

Nice tutorial, but here are some suggestions. I'd suggest if you're going to do an absolute beginner tutorial to do an absolute beginner tutorial. First, the command line stuff and javac stuff will confuse the people this tutorial is aimed at to begin with so you should explain how to set all that up or suggest ways around it like an IDE. Explaining every word is nice and all, but I think it would be more helpful to a beginner to learn the high level of what things mean first like what a class is, what a package is, where the heck are these packages coming from that you're importing, and maybe even a list of some other useful standard classes that can be imported and used.
  • 1


#635749 Sorting in array (advices, prompts needed)

Posted by Zer033x on 11 August 2012 - 02:21 AM

//Student.h
#include <iostream>

class Student
{
private:
int m_studentUniqueID;
int m_numErrorsMade;
int m_numLinesCoded;
public:
Student();
~Student();
void PrintStudentDetails();
//accessors
void SetStudentID(int id);
void SetStudentErrors(int err);
void SetStudentLines(int lines);
int GetStudentID();
int GetStudentErrors();
int GetStudentLines();
};

//Student.cpp
#include "Student.h"
Student::Student()
{
int m_studentUniqueID = 0;
int m_numErrorsMade = 0;
int m_numLinesCoded = 0;
}
Student::~Student()
{
}
void Student::PrintStudentDetails()
{
std::cout << "Student ID: " << m_studentUniqueID << " " << "Lines Coded: " << m_numLinesCoded << " " << "Code Errors: " << m_numErrorsMade << "\n";
}
void Student::SetStudentID(int id)
{
m_studentUniqueID = id;
}
void Student::SetStudentErrors(int err)
{
m_numErrorsMade = err;
}
void Student::SetStudentLines(int lines)
{
m_numLinesCoded = lines;
}
int Student::GetStudentID()
{
return m_studentUniqueID;
}
int Student::GetStudentErrors()
{
return m_numErrorsMade;
}
int Student::GetStudentLines()
{
return m_numLinesCoded;
}

//Random.h
// Random Number Class from Game Coding Complete book. Algorithm originally created by Takuji Nishimura and Makoto Matsumoto
#include <CMath>
#include <time.h>
#define CMATH_N 624
#define CMATH_M 397
#define CMATH_MATRIX_A 0x9908b0df /* constant vector a */
#define CMATH_UPPER_MASK 0x80000000 /* most significant w-r bits */
#define CMATH_LOWER_MASK 0x7fffffff /* least significant r bits */
/* Tempering parameters */
#define CMATH_TEMPERING_MASK_B 0x9d2c5680
#define CMATH_TEMPERING_MASK_C 0xefc60000
#define CMATH_TEMPERING_SHIFT_U(y) (y >> 11)
#define CMATH_TEMPERING_SHIFT_S(y) (y << 7)
#define CMATH_TEMPERING_SHIFT_T(y) (y << 15)
#define CMATH_TEMPERING_SHIFT_L(y) (y >> 18)
class RandomNumberGen
{
unsigned int rseed;
unsigned int rseed_sp;
unsigned long mt[CMATH_N];
int mti;
public:
RandomNumberGen();
~RandomNumberGen() {};
unsigned int Random(unsigned int n);
unsigned int Random();
void SetRandomSeed(unsigned int n);
unsigned int GetRandomSeed(void);
void Randomize(void);
void Delay(clock_t del);
};

//Random.cpp
#include "Random.h"
RandomNumberGen::RandomNumberGen(void)
{
rseed = 1;
mti=CMATH_N+1;
}
// Returns a number from 0 to n (excluding n)
unsigned int RandomNumberGen::Random( unsigned int n )
{
unsigned long y;
static unsigned long mag01[2]={0x0, CMATH_MATRIX_A};
if(n==0)
return(0);
/* mag01[x] = x * MATRIX_A for x=0,1 */
if (mti >= CMATH_N) { /* generate N words at one time */
int kk;
if (mti == CMATH_N+1) /* if sgenrand() has not been called, */
SetRandomSeed(4357); /* a default initial seed is used */
for (kk=0;kk<CMATH_N-CMATH_M;kk++) {
y = (mt[kk]&CMATH_UPPER_MASK)|(mt[kk+1]&CMATH_LOWER_MASK);
mt[kk] = mt[kk+CMATH_M] ^ (y >> 1) ^ mag01[y & 0x1];
}
for (;kk<CMATH_N-1;kk++) {
y = (mt[kk]&CMATH_UPPER_MASK)|(mt[kk+1]&CMATH_LOWER_MASK);
mt[kk] = mt[kk+(CMATH_M-CMATH_N)] ^ (y >> 1) ^ mag01[y & 0x1];
}
y = (mt[CMATH_N-1]&CMATH_UPPER_MASK)|(mt[0]&CMATH_LOWER_MASK);
mt[CMATH_N-1] = mt[CMATH_M-1] ^ (y >> 1) ^ mag01[y & 0x1];
mti = 0;
}
y = mt[mti++];
y ^= CMATH_TEMPERING_SHIFT_U(y);
y ^= CMATH_TEMPERING_SHIFT_S(y) & CMATH_TEMPERING_MASK_B;
y ^= CMATH_TEMPERING_SHIFT_T(y) & CMATH_TEMPERING_MASK_C;
y ^= CMATH_TEMPERING_SHIFT_L(y);
return (y%n);
}
void RandomNumberGen::SetRandomSeed(unsigned int n)
{
/* setting initial seeds to mt[N] using */
/* the generator Line 25 of Table 1 in */
/* [KNUTH 1981, The Art of Computer Programming */
/* Vol. 2 (2nd Ed.), pp102] */
mt[0]= n & 0xffffffff;
for (mti=1; mti<CMATH_N; mti++)
mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;
rseed = n;
}
unsigned int RandomNumberGen::GetRandomSeed(void)
{
return(rseed);
}
void RandomNumberGen::Randomize(void)
{
SetRandomSeed(time(NULL));
}
void RandomNumberGen::Delay(clock_t del)
{
clock_t end;
end = del + clock();
while (end > clock());
}

//Main.cpp
#include "Random.h"
#include <list>
#include "Student.h"
int main(int argc, char* argv[])
{
RandomNumberGen g_ran;
const int g_numStudents = 26;
const int g_numPotentialFlunkyStudents = 25;
int g_numErrorsBeforeFlunky = 50;
std::list<Student> g_allStudents;
std::list<Student> g_flunkyStudents;
for (int x = 1; x < g_numStudents; ++x)
{
time_t y = time(NULL);
g_ran.SetRandomSeed((int)y*x*x);
Student tempStudent[g_numStudents];
tempStudent[x].SetStudentID(x);
tempStudent[x].SetStudentLines(g_ran.Random(1000));
tempStudent[x].SetStudentErrors(g_ran.Random(100));
g_allStudents.push_back(tempStudent[x]);
}
std::cout << "\nThese are all the students in the class!\n\n";
int g_x = 0;
//display the student details and determine flunky students then add to flunky student list
for (std::list<Student>::iterator it = g_allStudents.begin(); it != g_allStudents.end(); ++it)
{
it->PrintStudentDetails();
if (it->GetStudentErrors() > g_numErrorsBeforeFlunky)
{

g_flunkyStudents.push_back(*it);
}

}
std::cout << "\nThe following students are flunky material that deserve to fail!\n\n";
//display flunky students that get complicated task
for (std::list<Student>::iterator it = g_flunkyStudents.begin(); it != g_flunkyStudents.end(); ++it)
{
it->PrintStudentDetails();
}

return 0;
}

  • 1


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download