The programming thread!

Way off-topic, and allowed! General discussions on anything and everything.

Moderator: EG Members

User avatar
Rolos
Tastes like burning!
Posts: 1001
Joined: Tue Jun 12, 2007 11:21 pm
Location: Los Angeles

The programming thread!

Post by Rolos »

I've heard many, many people saying that there's no "bad way" to program as long as you get the desired results.
This is, of course, complete and utter bullshit.
Let us take a look at a simple exercise, the likes of which you could find anywhere on the Internet:

EXAMPLE 37:
There are mathematical tools that allow us to determine whether a certain number is composed of the cubes of two others or not. For example, 35 is such a number, because it can be divided into 8 and 27, the cubes of 2 and 3, respectively.
Write a program that allows you to determine whether a number is a cube composite or not.

Sounds pretty simple, doesn't it?
Here, let's see what I did to solve the problem:

Code: Select all

import IIC1103Package.Usuario;
class Pruebas {

	public static void main(String[] args) {
		double n= Usuario.entero("Ingrese el numero que desee analyzar");
		double cube1=1;
		double cube2;
		double half= n/2;
		double root1;
		double root2;
		double integer_1;
		double integer_2;
		if(n%2==0){
			while (n!= cube1){
			cube1= half +1;
			cube2= half-1;
			root1= Math.cbrt(cube1);
			integer_1=root1*10%10;
			root2= Math.cbrt(cube2);
			integer_2=root2*10%10;
			if(integer_1==0 && integer_2==0){
			System.out.print("yes, it can.");
			n=cube1;}
			cube1++;
			cube2++;}}
		else{
			n= n-1;
			cube1= (n/2)+1;
			cube2= (n/2);
			while (n!=cube1){
				root1= Math.cbrt(cube1);
				integer_1=root1*10%10;
				root2= Math.cbrt(cube2);
				integer_2=root2*10%10;
			if(integer_1==0 && integer_2==0){
			System.out.print("yes, it can.");
			n=cube1;}
			else {cube1++;
			cube2= 1+n-cube1;}}}
}}
Now compare it to what some other guy did:

Code: Select all

public static boolean test(int n){
for(int i=1; i*i*i<=n; i++){
 int j;
 for(j=i+1; i*i*i+j*j*j<n; j++)
 if (i*i*i + j*j*j == n)
 return true;
 }
 return false;
 }
The question here being, is one of those answers wrong?
Of course one is.
Jesuschrist, don't be stupid.
Just look at that disgusting, convoluted, irregular, unstable piece of crap up there.
Of course it's wrong, and of course there is such a thing as bad programming.
Perhaps not impartially, objectively wrong, but wrong in the sense that answering an equation with a duck would be.

Anyway.
Does anyone feel like writing some code?
I think we could pull off a Berserk Quiz program, or something similar.
Hell, I think I could pull off something like that.
As a matter of fact, I'm starting right (well, tomorrow, I'm gonna study today) now.
Anyone, please, feel free to ridicule the code as I post it in this thread.
I encourage you to do so.
I'd particularly appreciate verbal abuse, that always pumps me up.

We have to start by thinking up the questions, I suppose.

Okay, first one:

1- Is the Idea of Evil canon?
A) No, of course not. The author requested that it be removed from the volumes, how much clearer can it be?
B) No, I don't think so. While consistent with the rest of the story, it has never been brought up since then, not even tangentially.
C) It is certainly a possibility, but I find it unlikely, as much I'd like it to be true. It had a very interesting implications, especially regarding the analogies that could have been drawn with the platonic theory of forms, but it just isn't canon.
D) DUR! Yes! Durdurdurdur! I like poop and approve of Nikita Khrushchev! Dadaism is a completely valid art movement! Oops, just soiled myself! Mmmm, just the way I like it!

And the correct answer would be B.
One original thought is worth a thousand mindless quotings.
~Diogenes of Sinope
User avatar
brunoafh
This is my new home
Posts: 206
Joined: Sun May 01, 2005 6:14 pm
Location: right over yonder

Re: The programming thread!

Post by brunoafh »

I would say C if it read as following: It is certainly a possibility, but I find it unlikely, as much I'd like it to be true. It had a very interesting implications, especially regarding the analogies that could have been drawn with the platonic theory of forms, but it just isn't canon.

I once made a controllable circle in QBasic, so I'll probably throw in my lame 2 cents if you continue to post some coding.
User avatar
DrPepperPro
Dirty Sennin
Posts: 2216
Joined: Mon Aug 20, 2007 5:16 pm

Re: The programming thread!

Post by DrPepperPro »

There are still things remaining of the IoE.

-The lost chapter is "God of the Abyss 2", and chapter 82 right before it being "God of the Abyss" is still in v13. It has Griffith sinking downward and thinking about all the people he sacrificed. He sinks into a vortex and sees a dark shape of the IoE and thinks "God...?".

-v24 when Flora is explaining stuff to Guts, she says in regard to the God Hand: "...they are the executors of the will of something lurking in the distant abyss of the astal world."

So while it may not exist as explained in c83, there are hints of some god-like thing in the vortex/abyss.


I was gonna say something about the programming, but I don't care anymore.
tsubaimomo
Augh! Bright sky fire burn eyes!
Posts: 375
Joined: Mon Nov 10, 2008 5:19 am
Location: To Elfhelm?

Re: The programming thread!

Post by tsubaimomo »

First of all, nobody wants to download your stupid little program, Mr. Poopy-pants Programmer, as we don't trust your alpaca-trojanhorse-tank-ridden executables.
Second and finally, you're all wrong and the Idea of Evil was never removed from cannon because I'm a Litteralist who takes the word of Miura straight from the original manuscripts, whereas you heretics worship you're so-called "tankoban" versions which have misinterpreted the true word.

Good day gentlemen (except that filthy mecha-animal that doesn't use comments)
Image
User avatar
War Machine
Tastes like burning!
Posts: 1463
Joined: Sun Apr 08, 2007 7:30 pm
Location: San Diego now

Re: The programming thread!

Post by War Machine »

Dude, have you tried doing graphical interfaces? They're a goddam nightmare.
tsubaimomo wrote:Second and finally, you're all wrong and the Idea of Evil was never removed from cannon because I'm a Litteralist who takes the word of Miura straight from the original manuscripts, whereas you heretics worship you're so-called "tankoban" versions which have misinterpreted the true word.
Jesus died for our sins and made the old scriptures obsolete. The true word of god is contained in the Dark Horse bible.
"Clearly my escape had not been anticipated, or my benevolent master would not have expended such efforts to prevent me from going. And if my departure displeased him, then that was a victory, however small, for me." - Raziel
User avatar
Rolos
Tastes like burning!
Posts: 1001
Joined: Tue Jun 12, 2007 11:21 pm
Location: Los Angeles

Re: The programming thread!

Post by Rolos »

Ha!
Sneak-post sucker-punch!
Caught your intellectual gonads unprepared, didn't I?
Wait. That sounded really gay.

Mmmmmnyway.
I was expecting Itsvan to jump at my obvious flame-bait, but it seems he's too mature for such petty squabbles.
....
WHAT A PUSSY, right?
I don't feel like writing much right now, so I'm going into laconic mode, but not before commenting on how brevity is the one and only defining quality of wit.
I'm not witty, so I can comment on the nature of wit. Fruit reference. Guayabas?
Disjointed.
[/laconic] Next question: Isidro's profession. What was he before hitting the road?
a) Hell if I know, I just read Berserk once.
b) I believe he did partake, at some point or another, on the reaping of earth's fruits, that is to say, in much simpler, shorter and easier to both vocalize and comprehend terms, he was a worker of the land, a farmboy.
c) Is this what I think it is? Am I not dreaming? Is this, really, truly, honest-to-god pointless trivia, inquiring about details so minuscule, so thoroughly and completely unimportant not even the auth-
d)Shut up! Just shut your goddamn mouth! Fuck's sakes, man, some of us actually want to answer! Jesus, what a dick. Oh, and the answer is "Who gives a flying fuck, it's not relevant to the story". Idiot.

And the answer is "None of the above" (actually a, but shhhhh!).

@Tsubaimomo: Thanks for the help, man. Thanks.
Also: Feh! Comments are for people who know what they're doing.
Last edited by Rolos on Thu Mar 31, 2011 3:48 am, edited 1 time in total.
One original thought is worth a thousand mindless quotings.
~Diogenes of Sinope
User avatar
DrPepperPro
Dirty Sennin
Posts: 2216
Joined: Mon Aug 20, 2007 5:16 pm

Re: The programming thread!

Post by DrPepperPro »

[/]
User avatar
Rolos
Tastes like burning!
Posts: 1001
Joined: Tue Jun 12, 2007 11:21 pm
Location: Los Angeles

Re: The programming thread!

Post by Rolos »

Ah, procrastination, how I loathe thee!
I'm doing it this thursday, I swear.
I just haven't had the time.
One original thought is worth a thousand mindless quotings.
~Diogenes of Sinope
User avatar
Rolos
Tastes like burning!
Posts: 1001
Joined: Tue Jun 12, 2007 11:21 pm
Location: Los Angeles

Re: The programming thread!

Post by Rolos »

Currently writing the code.
I'm not sure it's going to work (I've never used object arrays in java before) but I'll be damned if I don't try it.
Stay tuned, nonexistent reader!

P.S. Just started attending Japanese classes. Man, learning kanji and co. on my own was such a good idea. I don't know jack shit about grammar, but it gives me a big head-start over my classmates. I may even skip classes! Goddammit, I feel marginally better than usual.

1st Edit: Ok, got distracted for a minute there, sorry.
This is what I have so far:

+Problem analysis:
-Difficulty of questions changes according to correct:incorrect ratio = Acounter (variable)
-4 possible answers per question (instance variables)
-Static number of questions = N
-Question number= Qcounter (variable)
-Correct: Incorrect ratio Excellent = 0
-Correct: Incorrect ratio Competent = 0
-Correct: Incorrect ratio Deficient = 0
-Correct: Incorrect ratio Hilariously stupid = 0
-Number of correct answers = Acounter++ (variable)
- Final message depending on correct: incorrect ratio (output, variable within ranges)

+Program Specification:
~Input: Answers
~Process:
-Create Class
-Create one instance per question
-Make 4 object arrays
-Assign objects to arrays depending on difficulty
-Start Loop that works until Qcounter loop reaches N
-First Question
-Loop that adds determined number of units to Incorrect:Correct ratio per iteration
-Conditional if (Acounter =< Excellent II Acounter > Competent){ select object 1 from array Excellent}
-Conditional if (Acounter =< Competent II Acounter > Deficient){ select object 1 from array Competent}
-Conditional if (Acounter =< Deficient II Acounter > Hilariously_stupid){ select object 1 from array Deficient}
-Conditional Else {Print (Select object 1 from array hilariously_Stupid}
-Loop ends
~Output: Print Congratulations or laugh at user depending on Acounter value
+Code...pending.

2nd EDIT: Ok. I want this to be clear. I am not writing the code today because I'm lazy. It's not because I have other things to do, even though I do.
I'm just a fucking slob.
One original thought is worth a thousand mindless quotings.
~Diogenes of Sinope
User avatar
War Machine
Tastes like burning!
Posts: 1463
Joined: Sun Apr 08, 2007 7:30 pm
Location: San Diego now

Re: The programming thread!

Post by War Machine »

Here's some advice, name your variables according to their function or role in the code. Acounter and Qcounter are awfully vague and only tell me that they're gonna count something but not what or for what purpose. Also, the convention is that variable names start with a lowercase letter (e.g. aCounter) and reserve words starting with an uppercase letter for methods and classes. And instance variables, as their name implies, are variables that are instantiated at the beginning of the program or object, used only for the duration of it. For example:

Code: Select all

public class MyLittlePony
{
     private int age; //Instance variable to be populated later if MyLittlePony class and the default constructor are called.
     private String name; //Another instance variable

     public MyLittlePony() //Default constructor for class MyLittlePony
     {
          name = "Fluffy";
          age = 5;
     }

}
If during my main method I don't call upon the MyLittlePony class, then instance variables age and name don't get instantiated.

Another thing, if you plan to use multiple, consecutive if-statements, you might want to consider using something more appropriate like "switch":

http://download.oracle.com/javase/tutor ... witch.html

I'll try not to say anything else, I wanna see what you come up with.

PS: Speaking of conventions, there's one convention I don't agree on, and that's putting the opening bracket of a function right next to the name of the function. I prefer to put it on the next line so that the open and close brackets have the same indentation and are easier to find when reading through the code.
"Clearly my escape had not been anticipated, or my benevolent master would not have expended such efforts to prevent me from going. And if my departure displeased him, then that was a victory, however small, for me." - Raziel
User avatar
brunoafh
This is my new home
Posts: 206
Joined: Sun May 01, 2005 6:14 pm
Location: right over yonder

Re: The programming thread!

Post by brunoafh »

Rolos wrote:P.S. Just started attending Japanese classes
がんばってロロスさん。
somefatman
notanewb
Posts: 73
Joined: Tue Feb 22, 2011 4:40 pm

Re: The programming thread!

Post by somefatman »

@War Machine
I used to agree with placing the brackets on the next line to match opening and closing, but then I worked with a programmer who was really anal about following the standards so I learned to deal with it.
@Rolos
If you have any problem with using object arrays, you may want to look at the ArrayList Class. ArrayList allows for dynamic size arrays,is probably easier to manage, and has useful searching features.
I dont know if you know anything about generics but to make your code extra type safe you can lock the object type in your ArrayList and other enumerations:

Code: Select all

ArrayList<String> stringList;
stringList = new ArrayList<String>();
This makes the ArrayList only accept String objects (or String subclasses) so you can be guaranteed that anything that you later remove from the array is of type String. This is obviously more useful for larger projects, but its never to early to learn proper coding.
User avatar
War Machine
Tastes like burning!
Posts: 1463
Joined: Sun Apr 08, 2007 7:30 pm
Location: San Diego now

Re: The programming thread!

Post by War Machine »

somefatman wrote:@War Machine
I used to agree with placing the brackets on the next line to match opening and closing, but then I worked with a programmer who was really anal about following the standards so I learned to deal with it.
Yeah, I don't do it when I'm working with other people if they don't like it. I'll go with the group if that's what they prefer (so far no one has disagreed though). But the code becomes substantially easier to read if I do it this way. I mean, look at this and tell me what's easier to understand:

Code: Select all

Something() {
     int a = 1;
     if(a == 1) {
          int b = 4;
          if(b == 4) {
               int c = 0;
          }
          int d = 8;
     }
     int e = 2;
}

Code: Select all

Something()
{
     int a = 1;
     if(a == 1)
     {
          int b = 4;
          if(b == 4)
          {
               int c = 0;
          }
          int d = 8;
     }
     int e = 2;
}
"Clearly my escape had not been anticipated, or my benevolent master would not have expended such efforts to prevent me from going. And if my departure displeased him, then that was a victory, however small, for me." - Raziel
User avatar
Rolos
Tastes like burning!
Posts: 1001
Joined: Tue Jun 12, 2007 11:21 pm
Location: Los Angeles

Re: The programming thread!

Post by Rolos »

Are you guys talking about object indexes? Because that's how my textbook describes object arrays. I've been thinking of them as remote controls, that's how they work, right?
Anyway, I'm almost done with the code.
I'll upload it tomorrow, once I'm done with fucking chemistry.
Don't get me wrong, I love stoichiometry, I'm just so fucking sick of the subject and how goddamned 'loose' it is.
Oh, and by the way, do you guys have any recommendations for graphics packages? Lots of them out there.
Also, working with happy numbers.
Mathematicians have weird naming tendencies.
One original thought is worth a thousand mindless quotings.
~Diogenes of Sinope
somefatman
notanewb
Posts: 73
Joined: Tue Feb 22, 2011 4:40 pm

Re: The programming thread!

Post by somefatman »

What do you mean by Object indexes. An object Array would be:

Code: Select all

String[] stringArray = new String[5]; \\or do I want 4 can never remember if this number counts 0
stringArray[0] = "hi";
stringArray[1] = "Rolos";
stringArray[2] = "how";
stringArray[3] = "are";
stringArray[4] = "you?";
for(int i=0, i<stringArray.length, i++){
    System.out.print(stringArray[i]);
}
You can also create the array using the shortcut:

Code: Select all

String[] stringArray = {"hi","Rolos","how","are","you?"};
You could do the same thing with ArrayList, but the list wont have a fixed size and you could use methods to add objects to the array in addition to using indexes. There are also search tools (the contains method, the indexOf method). Has anyone linked you the API yet: http://download.oracle.com/javase/1.5.0 ... mmary.html

Code: Select all

ArrayList<String> stringArray = new ArrayList<String>();
stringArray.add("hi");
stringArray.add("Rolos");
stringArray.add("how");
stringArray.add("are");
stringArray.add("you?");
for(String s:stringArray){  //foreach loop  read as: for each String s in array stringArray
    System.out.print(s);
}
if(stringArray.contains("Rolos"){
    System.out.print("Found Rolos");
    stringArray.remove("Rolos");
    System.out.print("All gone");
}


For graphics, it depends on what you are trying to accomplish.
If you want to make a basic GUI with buttons, entry fields, pictures, etc. then the Java Swing API is will be more than sufficient. Swing is included with what ever JDK you have. If you want to work with Swing I also recommend you use the Netbeans IDE since it includes a really useful GUI builder that makes absolute positioning a breeze.
If you want something more like game graphics, well then im not really the one to ask. Ive have done some real simple stuff (Pong, a bad side scroller than never got past the characters sidescrolling) still using Swing/AWT (AWT being the older graphics package for java). Anything more advanced and I really don't know. I have heard good things about OpenGL but have never used it myself.
User avatar
Rolos
Tastes like burning!
Posts: 1001
Joined: Tue Jun 12, 2007 11:21 pm
Location: Los Angeles

Re: The programming thread!

Post by Rolos »

FUCK!
Can't make it today.
Goddammit, this is so unprofessional.
The sad part is that I can't claim I failed to post the code for some good reason, like a test or something.
It's just me being a fucking slob, and doing tangential, unproductive bullshit.
Won't promise I'll upload it tomorrow, because then I'll feel bad if I don't.
Then again, I deserve to feel bad.
I spent most of the day re-reading old comics, just lying in the fucking carpet.
Goddammit!
I'm getting all pumped up.
This is good.
Very good.
There's no greater force in this world than regret.
Pure, untainted regret at squandering life's many opportunities.
Fuck it, I'm making some coffee.
I.
Will.
Finish.
This.
Shit.
One original thought is worth a thousand mindless quotings.
~Diogenes of Sinope
User avatar
DrPepperPro
Dirty Sennin
Posts: 2216
Joined: Mon Aug 20, 2007 5:16 pm

Re: The programming thread!

Post by DrPepperPro »

Rolos wrote:[laconic] {
FUCK!
Goddammit,
Goddammit!
This is good.
coffee.
I.
Will.
Shit.
}
[/]
tsubaimomo
Augh! Bright sky fire burn eyes!
Posts: 375
Joined: Mon Nov 10, 2008 5:19 am
Location: To Elfhelm?

Re: The programming thread!

Post by tsubaimomo »

The Programming is strong with this one.
Image
User avatar
Rolos
Tastes like burning!
Posts: 1001
Joined: Tue Jun 12, 2007 11:21 pm
Location: Los Angeles

Re: The programming thread!

Post by Rolos »

No it's not.
I suck.
Oh, the shame!
This Friday, I swear.
I hate self-imposed deadlines!

P.S. This Friday is Gauss day at my house! Woohooo! You're all invited to the southern hemisphere for the day!
One original thought is worth a thousand mindless quotings.
~Diogenes of Sinope
User avatar
dialdfordesi
I live in a giant bucket.
Posts: 907
Joined: Sat Jul 02, 2005 9:52 pm
Location: Bumblefuck, Midwest.

Re: The programming thread!

Post by dialdfordesi »

Gotta get down on Friday!
Trust me, I'm a doctor!
User avatar
War Machine
Tastes like burning!
Posts: 1463
Joined: Sun Apr 08, 2007 7:30 pm
Location: San Diego now

Re: The programming thread!

Post by War Machine »

Partying, partying, fun, fun, fun, fun.
"Clearly my escape had not been anticipated, or my benevolent master would not have expended such efforts to prevent me from going. And if my departure displeased him, then that was a victory, however small, for me." - Raziel
User avatar
Mail
Mastered PM
Posts: 136
Joined: Sat Sep 19, 2009 3:32 am
Location: The far side of the moon

Re: The programming thread!

Post by Mail »

Did you say you're doing it in Java? Is it possibly going to get added to the site?!? It can be Rolos' inane trivia page! I would be so down with that.

Also, what's up with the graphics? You figure out what you're using yet?
User avatar
Rolos
Tastes like burning!
Posts: 1001
Joined: Tue Jun 12, 2007 11:21 pm
Location: Los Angeles

Re: The programming thread!

Post by Rolos »

Remember when I said I had most of the code done?
Turns out I'm an idiot and had 2 separate programs in one. In my mind, the Questionaire-O-Matic program was the same as the Questionaire-O-Creator program.
Oh, spagueti, oh.
Big fuck up there.
But never fear, my fellow maggots!
I'm totally gonna fix that.
And I'll post it the code here, then figure out how to do the graphical interface and presto!
The end of times will be upon us.
I'm stretching what little I know about java to do this, so, please, don't laugh too hard when seeing the code.
It may be pernicious to your health.
The code will be done in 6-7 hours, tops. //EDIT: (hahahhahaha, no. Fell asleep)

No it won't.
Yes it will!
Nope.
Shut up and write the goddamn code, you slut.
Ok.

EDIT 1:
I'm doing 35 questions. Less than that and it's too short, and more would be tedious to answer.

EDIT 2:
This is what I have so far:

(this is the class I'll use for the questions)

Code: Select all

public class triviaQuestion_N {
	/**instance variables:**/
	String questionN;
	String answerA;
	String answerB;
	String answerC;
	String answerD;
	boolean a, b, c, d;
	int correctAnswer;
	int levelOfDifficulty;
	// question: Why am I always told that I should make these private?
	
public triviaQuestion_N(String N, String A, String B, String C, String D, int value, int easy_hard){
	/**constructor:**/
	questionN = N;
	answerA = A;
	answerB = B;
	answerC = C;
	answerD = D;
	levelOfDifficulty = easy_hard;
	a = b = c = d = false;
	if (value == 1){
		a= true;
	}//if user selects alternative 1 as the correct one
	else {if(value == 2){
		b= true;
	}//if user selects alternative 2 as the correct one
	else {if(value == 3){
		c= true;
	}//if user selects alternative 3 as the correct one
	else {if(value == 4){
		d= true;
	}//if user selects alternative 4 as the correct one}
	}//else 3
	}//else 2
	}//else 1
	}//constructor
    
public void TheCorrectAnsweris (){
	if (a==true){
		System.out.print(a);
	}//if1 
	if (b==true){
		System.out.print(b);
	}//if2 
	if (c==true){
	System.out.print(c);
	}//if3 
	if (d==true){
		System.out.print(d);
	}//if3 
    }//TheCorrectAnsweris method
    }//class


(This is the class I'll use to create the questionaire)

Code: Select all

public class Questionaire_O_Maker {
 public triviaQuestion_N[] excelent;
 public triviaQuestion_N[] competent;
 public triviaQuestion_N[] defficient;
 public triviaQuestion_N[] functionallyRetarded;
 public int numberOfQuestions;
 
 public Questionaire_O_Maker (int questionNumber){
	 numberOfQuestions= questionNumber;
 }//constructor
 
 public void Create_Questionaire(){
	 excelent = competent = defficient = functionallyRetarded = new triviaQuestion_N[numberOfQuestions];
	     for (int questionsWrittenSoFar=0; questionsWrittenSoFar<=numberOfQuestions; questionsWrittenSoFar++){
		 excelent = new triviaQuestion_N [questionsWrittenSoFar];
		 competent = new triviaQuestion_N [questionsWrittenSoFar];
		 defficient = new triviaQuestion_N [questionsWrittenSoFar];
		 excelent = new triviaQuestion_N [questionsWrittenSoFar];
		 functionallyRetarded = new triviaQuestion_N [questionsWrittenSoFar];
	 }//for
	     System.out.print("Done with the questions!");
 }//method
 
}//class
The whole 'assign each individual question to a particular array according to difficulty' concept kinda went out the window.
Probably going to incorporate it later.

Ok, gotta keep going.

EDIT3: Fuck! I screwed big time.

EDIT4: Ok, I fixed the problem.
Man, arrays are difficult to manage.
Here's the fixed version:

Code: Select all

import IIC1103Package.Usuario;
public class Questionaire_O_Maker {
 public triviaQuestion_N[] excelent;
 public triviaQuestion_N[] competent;
 public triviaQuestion_N[] defficient;
 public triviaQuestion_N[] functionallyRetarded;
 public int numberOfQuestions;
 
 public Questionaire_O_Maker (int questionNumber){
	 questionNumber= Usuario.entero("Enter the desired number of questions");
	 numberOfQuestions= questionNumber;
 }//constructor
 
 public void Create_Questionaire(){
	 excelent = competent = defficient = functionallyRetarded = new triviaQuestion_N[numberOfQuestions];
	     for (int questionsWrittenSoFar=0; questionsWrittenSoFar<numberOfQuestions; questionsWrittenSoFar++){
		 excelent [questionsWrittenSoFar] = new triviaQuestion_N();
		 Usuario.mensaje(questionsWrittenSoFar+1);
	 }//for1
	     Usuario.mensaje("Questions of difficulty 4 are done!");
	     for (int questionsWrittenSoFar=0; questionsWrittenSoFar<=numberOfQuestions; questionsWrittenSoFar++){
	    	 competent [questionsWrittenSoFar]= new triviaQuestion_N() ;
			 Usuario.mensaje(questionsWrittenSoFar+1);
		 }//for2
	     Usuario.mensaje("Questions of difficulty 3 are done!");
	     for (int questionsWrittenSoFar=0; questionsWrittenSoFar<=numberOfQuestions; questionsWrittenSoFar++){
	    	 defficient [questionsWrittenSoFar]= new triviaQuestion_N();
			 Usuario.mensaje(questionsWrittenSoFar+1);
		 }//for3
	     Usuario.mensaje("Questions of difficulty 2 are done!");
	     for (int questionsWrittenSoFar=0; questionsWrittenSoFar<=numberOfQuestions; questionsWrittenSoFar++){
	    	 functionallyRetarded [questionsWrittenSoFar]= new triviaQuestion_N();
			 Usuario.mensaje(questionsWrittenSoFar+1);
		 }//for4
	     Usuario.mensaje("Questions of difficulty 1 are done!");
		  
	     System.out.print("Done with the questions!");
 }//method
 }//class

Sorry if it seems awfully inefficient, I'm going to try to fix that once the damn thing works.
Which reminds me of a cool thing I once read about engineering: That you're finished not when there's nothing to add, but when there's nothing to take away.

EDIT 5: I just realized I've been making a program in which none of the questions is saved for more than one run. Screw this, I'm just going to make a program specially for Berserk, and then write a more general form, once I figure out how to save the values I assign to objects.

EDIT 6: Does anyone know where I can get the API needed for serialization?
If I understand what I read online correctly, that application would allow me to save the value of the objects I create with the Questionaire-O-Maker, which I could use as input for the quiz program.
And the Berserk quiz would be my first.
I'm saving myself for it.

EDIT 7: It was part of the core package. Dur. Proceeding to figure out how it works.

EDIT 8 and final:
This is the last edit of the day. It's late and I've got other things to do.
Can't say I'm satisfied with what I managed to do, but it's something.
Here's what I believe is the final code for the Questions and Trivia-Builder programs:
[spoiler]

Code: Select all

import IIC1103Package.Usuario;
import java.io.Serializable;


public class triviaQuestion_N implements Serializable{ [b]//makes the class serializable, which is kind of important later on.
	/**instance variables:**/[/b]
	String questionN;
	String answer1;
	String answer2;
	String answer3;
	String answer4;
	boolean a, b, c, d;
	int correctAnswer;
	int levelOfDifficulty;
[b]	// question: Why am I always told that I should make these private?[/b]
	
public triviaQuestion_N(){
	[b]/**constructor:**/[/b]
	questionN= Usuario.texto("Write question here");
	answer1= Usuario.texto("Write answer 1 here");
	answer2= Usuario.texto("Write answer 2 here");
	answer3= Usuario.texto("Write answer 3 here");
	answer4= Usuario.texto("Write answer 4 here");
	correctAnswer= Usuario.entero("What is the correct answer?)");
	a = b = c = d = false;
	if (correctAnswer == 1){
		a= true;
	}[b]//if user selects alternative 1 as the correct one[/b]
	else {if(correctAnswer== 2){
		b= true;
	}[b]//if user selects alternative 2 as the correct one[/b]
	else {if(correctAnswer == 3){
		c= true;
	}[b]//if user selects alternative 3 as the correct one[/b]
	else {if(correctAnswer == 4){
		d= true;
	}[b]//if user selects alternative 4 as the correct one}[/b]
	}[b]//else 3[/b]
	}[b]//else 2
	}//else 1
	}//constructor[/b]
    
public void TheCorrectAnsweris (){
	if (a==true){
		System.out.print(a);
	}[b]//if1 [/b]
	if (b==true){
		System.out.print(b);
	}[b]//if2 [/b]
	if (c==true){
	System.out.print(c);
	}[b]//if3 [/b]
	if (d==true){
		System.out.print(d);
	}[b]//if3[/b] 
    }//TheCorrectAnsweris method

public void TheQuestionis (){
	System.out.print(questionN);
}[b]//Method to print out the question being asked.[/b]

public void ThePossibleAnswersare(){
	System.out.println("A) "+ answer1);
	System.out.println("B) "+ answer2);
	System.out.println("C) "+ answer3);
	System.out.println("D) "+ answer4);
}[b]//method to tell you what the alternatives are.[/b]

}[b]//class[/b]


[/spoiler]

[spoiler]

Code: Select all

 import IIC1103Package.Usuario;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class Questionaire_O_Maker implements Serializable { [b]//makes the program serializable, which allows the user to save the questions/answers he/she writes[/b]
 public triviaQuestion_N[] excelent;
 public triviaQuestion_N[] competent;
 public triviaQuestion_N[] defficient;
 public triviaQuestion_N[] functionallyRetarded;
 public int numberOfQuestions;
 
 public Questionaire_O_Maker (){
	 numberOfQuestions= Usuario.entero("Enter the desired number of questions");
	 excelent = competent = defficient = functionallyRetarded = new triviaQuestion_N[numberOfQuestions];
	     for (int questionsWrittenSoFar=0; questionsWrittenSoFar<numberOfQuestions; questionsWrittenSoFar++){
		 excelent [questionsWrittenSoFar] = new triviaQuestion_N();
		 Usuario.mensaje(questionsWrittenSoFar+1);
	 }[b]//for1 forces a discrete number of iterations, making an array of trivia questions in the process. This array contains all ridiculously difficult questions [/b]
	     Usuario.mensaje("Questions of difficulty 4 are done!");
	     for (int questionsWrittenSoFar=0; questionsWrittenSoFar<numberOfQuestions; questionsWrittenSoFar++){
	    	 competent [questionsWrittenSoFar]= new triviaQuestion_N() ;
			 Usuario.mensaje(questionsWrittenSoFar+1);
		 }[b]//for2 forces a discrete number of iterations, making an array of trivia questions in the process. This array contains all moderately difficult questions [/b]
	     Usuario.mensaje("Questions of difficulty 3 are done!");
	     for (int questionsWrittenSoFar=0; questionsWrittenSoFar<numberOfQuestions; questionsWrittenSoFar++){
	    	 defficient [questionsWrittenSoFar]= new triviaQuestion_N();
			 Usuario.mensaje(questionsWrittenSoFar+1);
		 }[b]//for3 forces a discrete number of iterations, making an array of trivia questions in the process. This array contains all normal questions [/b]
	     Usuario.mensaje("Questions of difficulty 2 are done!");
	     for (int questionsWrittenSoFar=0; questionsWrittenSoFar<numberOfQuestions; questionsWrittenSoFar++){
	    	 functionallyRetarded [questionsWrittenSoFar]= new triviaQuestion_N();
			 Usuario.mensaje(questionsWrittenSoFar+1);
		 }[b]//for4 forces a discrete number of iterations, making an array of trivia questions in the process. This array contains all easy questions [/b]
	     Usuario.mensaje("Questions of difficulty 1 are done!");
	     System.out.print("Done with the questions!");
 }//constructor

 

 public static void main(String[] args) {
		          
		Usuario.mensaje("Introduction to Quiz-writing program"); [b]//Here you write whatever inane bullshit you'll greet the user with.[/b]
		Usuario.mensaje("Instructions"); [b]//pretty much self-explanatory[/b]
		Questionaire_O_Maker writeNameOfQuestionaireHere = new Questionaire_O_Maker();[b]// I'll change this part so the user can decide on the name of the questionaire	 [/b]
	   
	     try{
	    	 FileOutputStream filestream = new FileOutputStream ("writeNameOfQuestionaireHere.ser");
		     ObjectOutputStream os = new ObjectOutputStream (filestream);
	         os.writeObject(writeNameOfQuestionaireHere);
		     os.close();[b]// these 3 lines save all info pertaining to the objects created when this program was executed. [/b]
	       }
	     catch(IOException ex){
	       ex.printStackTrace();
	      }//catch exceptions
	   
 }//main
 
 



}//Questionaire_O_Maker class 

[/spoiler]

And here the actual Trivia program, where I've been stuck for the last couple of hours.

[spoiler]

Code: Select all

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import IIC1103Package.Usuario;
public class Questionaire_O_Matic {
	public static void main(String[] args) {
	try{
			ObjectInputStream os = new ObjectInputStream (new FileInputStream ("writeNameOfQuestionaireHere.ser")); 
		     Questionaire_O_Maker Trivia = (Questionaire_O_Maker) os.readObject();} [b]//this restores the values of the questionaire you made (or someone else made and unploaded to some random forum) and makes a new set of objects out of them, the ones that will be used throughout the program.[/b]
	     catch(IOException ex){
	       ex.printStackTrace();
	      }//catch exceptions
        catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Usuario.mensaje("Introduction to Trivia"); //Here you write whatever inane bullshit you'll greet the user with.
		Usuario.mensaje("Instructions"); //pretty much self-explanatory
		excelent[1].theCorrectAnsweris();
[b]And this is where I'm stuck. I have no idea why is it that I get a message telling me that whatever object I use from the file is an invalid variable. I know the values are being loaded, but I can't use them. It's very frustrating. [/b]
		
		
[/spoiler]

And that's that.
Hail Satan.
One original thought is worth a thousand mindless quotings.
~Diogenes of Sinope
somefatman
notanewb
Posts: 73
Joined: Tue Feb 22, 2011 4:40 pm

Re: The programming thread!

Post by somefatman »

The reason you make the fields private is so they are not messed with by outside code. This way, when working with the values you can be sure of the values and how they will behave. Java objects are supposed to be "black boxes" meaning that another coder need not know how they work on the inside. You expose the variables to outside manuipulation through the use of getter and setter methods where you can place checks on the data as needed:

Code: Select all

private int test;
...
public int getTest(){
   return test;
}
public void setTest(int test){
   //test data as appropriate
   this.test = test;
}
--EDIT--
Looking at the serialization API I don't see why your code is not working. What is the exact error your getting? Have you run your code with a debugger? Possibly try writing out a single question and seeing if you can read that in.
PS I would try to run your code but I have no idea what package "IIC1103Package.Usuario;" is so I dont think your code would not run on my machine.
User avatar
Mail
Mastered PM
Posts: 136
Joined: Sat Sep 19, 2009 3:32 am
Location: The far side of the moon

Re: The programming thread!

Post by Mail »

Yeah...what the hell is usuario? Never heard of it. Looks like some kind of foreign display package I guess? Anyways, I aint gots what I need to compile it and can't find it easily, so I won't be much help troubleshooting it.

Also, one of the most awesome things about Java is the ability to create applets. You might consider converting the main class into an applet and asking EG pretty please to host it :D
Post Reply