Computer Science Canada A Loop program |
Author: | grasshopper [ Mon Mar 29, 2004 4:38 pm ] |
Post subject: | A Loop program |
i need help writing a program.. i`m just testing loops.. and i`m not sure how to print this but i just want to try.. anyone know the code? I need to write a program which produces sets of triangles like these: **** *** ** * * ** *** **** - ->I need to allow the user to deteremine how many sets of triangles will be produced.. I know I need this using TextIO... I started programming this using a `for loop`. and then got stuck.... any suggestions? thanksss |
Author: | Tony [ Mon Mar 29, 2004 5:08 pm ] | ||||
Post subject: | |||||
well first you'll need a function to compile your line of astrics, similar to turing's repeat
now break your patten into decending and addending loops
|
Author: | wtd [ Mon Mar 29, 2004 6:03 pm ] | ||
Post subject: | |||
Good suggestion, tony, but instead of using String and + all over the place, use StringBuffer.
It minimizes the number of temporary String objects created and increases memory efficiency. |
Author: | grasshopper [ Tue Mar 30, 2004 8:37 pm ] | ||
Post subject: | |||
umm.. i`m not worrying about adding the *`s for now because i don`t know where exactly to put that in my code.. i`ll try figuring that out later... but i have another question... okay this program it`s suppose allow the user to determine how many sets... not how long the set is.. ![]()
the following gets printed out.. ![]() 5 4 3 2 1 1 2 3 4 5 ![]() 4 3 2 1 1 2 3 4 and so on but want i need it to print is.. ![]() **** *** ** * * ** *** **** **** *** ** * * ** *** **** **** *** ** * * ** *** **** **** *** ** * * ** *** **** **** *** ** * * ** *** **** like print that whole set five times if they chose 5... if anyone can help that would be great.. ![]() thanks |
Author: | Tony [ Tue Mar 30, 2004 10:29 pm ] | ||
Post subject: | |||
![]()
![]() |
Author: | grasshopper [ Thu Apr 01, 2004 9:37 am ] | ||
Post subject: | thanks... | ||
heey.. thanks for the help guys.. but i finally figured it out!!
![]() |
Author: | wtd [ Thu Apr 01, 2004 3:54 pm ] |
Post subject: | |
Now put all of the loops and such in a separate method. Putting everything in main is bad, bad bad! ![]() |
Author: | grasshopper [ Fri Apr 02, 2004 1:16 am ] |
Post subject: | |
lol is it?.. meh my teacher formatted it like that... |
Author: | wtd [ Fri Apr 02, 2004 3:09 pm ] | ||
Post subject: | |||
Yes. Teachers aren't always right. ![]()
|
Author: | grasshopper [ Sat Apr 03, 2004 2:08 pm ] |
Post subject: | |
oo i c.. umm okay would you be able to help me with something else..as i`m still a beg. at programming.. i want to try this.. like see the code and see if it can help with something else i need to figure out..but not wanting the code for that.. hmm lets see... alright.. how about a program that asks the user to enter 10 marks for a student.. umm then print out the student`s average, the maximum mark and the minumum mark..using a loop.. using only one variable to read all marks can you help ![]() |
Author: | Dan [ Sat Apr 03, 2004 3:08 pm ] |
Post subject: | |
well to start you will need 3 varibales. an int for the max var an int for the min var an int for the sum of all the marks now you will input the 1st mark and sotre it in all 3 vars (max, min, and sum) next you will make a loop that runs for number of marks to be inputed -1 (becuse you all ready got the 1st mark). then in the loop you input the number in a temp var. add it to sum and check to see if it is bigger then max or smaller then min. if it is you will set it as the one it is biger or smaller then. then end the loop and output the max, the min and the sum div the num of marks to get the avg. |
Author: | wtd [ Sat Apr 03, 2004 7:54 pm ] | ||
Post subject: | |||
You'd want something like the following untested program.
And just in case seeing the problem solved in other languages can be of help, I've attached some other samples. |
Author: | grasshopper [ Sun Apr 04, 2004 3:47 pm ] |
Post subject: | |
uuuhhh.. what? i don`t think it was suppose to get that confusing.. ![]() |
Author: | grasshopper [ Sun Apr 04, 2004 9:14 pm ] | ||||
Post subject: | |||||
okaay umm how about i forget that program.. i confused myself.. how about.. A program that asks the user to enter a mark. The program then prints out one line. The word and the mark will be separated by enough dots so that the total line length is 30: Enter your name: Super Enter your mark: 95 Super's mark is"¦"¦"¦..95 - ->to print the dots, use System.out.print(".") inside a loop body - ->might need the following code
i don`t know how i`d program that and if i`d use a while or for loop this is my strcutre so far. (without the public static void main...etc.. etc..)
![]() ![]() |
Author: | Dan [ Mon Apr 05, 2004 4:28 pm ] | ||||
Post subject: | |||||
TextIO eh? i dont think that is a standered I/O method, u using the Robot java text book by any chance? any way that dose not matter. This is what i think u need:
in this part:
length is a var to rep how many chars are in the line minus the "."s. u have to add in 10 for the "'s mark is" part and 1, 2 or 3 for the length of the mark. the for loop part runs for 30 - the length of the line with out "."s and thous will put in the right number of "."s. some error checking should probly be added to this in case there is - dots or 0 dots needed. by the way what corse u talking and where? that TextIO is alot like one of the methods in a book i was using for my course. |
Author: | wtd [ Mon Apr 05, 2004 9:27 pm ] | ||||||||||||||||||||
Post subject: | |||||||||||||||||||||
Analysis Well, first let's analyze the problem. You need to take two pieces of data (the person's name, and their grade) and output a string containing both values, separated by enough characters (either "." or something else, perhaps). So we want a function... a black box we push data into, and take information out of. What we put in is a name and a grade, and we get out a string.
That's just a shell, but when it comes to using the function, that's all we'll need to know. Of course, for it to be useful, you need something between the brackets. This is what you're asking about. So first we need to figure out the length of the name and the length of the grade (it's probably between 0 and 100, so it might be 1 or 3 printed characters).
So now we know how how much of those 30 characters the two pieces of data will take up in the end result. Thus we can figure out the number of dots we'll need.
So now we can just start at 1 and loop up until we get to that amount. We'll create a variable to store the dots in.
Now, to compose the final result, we simple add the dots to the name, and the grade to the end of that. Then we take that string and return it from our function. We "spit" it out the other side of our "black box".
We can then use this as follows:
Object-Oriented Design Further analyzing this problem, the two pieces of data are related. Together they describe a student. This is our "thing" for this program. What we do to this thing is take the data that represents it (name and grade) and stick them together in a certain way. So we have a thing (or "object") in the form of a student, and an action ("method") in the form of getting our formatted string. We need a way to describe a student object, and the action that it can perform on itself. In Java (and many other languages) we call this a "class". The Student class now has attributes called name and grade. These we say are private to the class. That is, only a student can know its own name and grade. It can elect to share that information with others, but they cannot access it without permission.
Ok, so that's all well and good, but this doesn't do much of anything at all, and nothing useful as far as we're concerned. We can, however, create a new student that will have a name and a grade. Of course, since we haven't provided any information as to what those are, the student object will be meaningless. To provide some initial information, we need a special method called a "constructor". The constructor will take as input an initial name, and an initial grade, and what makes it special is that it doesn't return anything. Not even "void". Of course, the constructor is "public", so that anyone can create a new student. Inside the constructor, we'll set the student's name and grade. A note on Java, specifically: the constructor method always has the same name as the class.
So now we can create a new Student in our "main" method.
Again, we can't really do anything with this, so we have to add a public method to get our specially formatted string, as detailed in the getFormattedString method. In the case of the Student class, we'll use the method name toString. This is the method that is, by convention, used when another class is trying to figure out what an object looks like as a string. For instance, System.out.println.
|
Author: | grasshopper [ Mon Apr 05, 2004 10:29 pm ] |
Post subject: | |
ooh my gosh wtd.. you just summarized wht my student teacher has been tryin to teach us for like two weeks.. i understand it now!.. you guys are SOOO cool! but i`m taught to put everything in the main.. that`s how my teacher has been teaching us.. that`s stupid if were going to learn to do it the right way later (in university maybe..(the way you guys are).. Hacker Dan i`m using BlueJ to run the programs.. and its a ICS3M1 course.. the programs that i`ve been asking for assistant on are not what is needed for the course assignments but similar to help me out a little more.. and not actually get the code for the program (as i should be able to program myself) ..reasons why i find it harder is i guess because i advanced a grade in computer science..and my teacher (student teacher actually.that`s been teaching right now.. has an accent so it`s very hard to understand).. In Newmarket near Toronto (noo idea where you`re from so incase you don`t know)... in Ontario of course) |
Author: | Dan [ Tue Apr 06, 2004 3:55 pm ] |
Post subject: | |
ah i see, i am doing the grade 12 level of compsci, alougth i fished the noraml cruimale stuff along time ago and am working on amsents from a book they uses at 1st year U of W and some of there permade methods are simmer to the BlueJ ones i gusse. ya i can be hard to lrean compsci from a teacher that is unschure of them there self. alougth i chage schools this year so i fainly have a compsci teacher who knows what OOP even means ![]() |
Author: | wtd [ Tue Apr 06, 2004 9:36 pm ] | ||||||||||||||||
Post subject: | |||||||||||||||||
Glad I could help. Does this explanation shed any light on my proposed solution for your previous question about inputting ten grades and then finding the max, min, and average? In the event it doesn't, and because I'm sitting here bored while I wait the requisite 30 minutes before swimming after eating, an analysis that should make it make sense. Analysis The problem involves inputting ten grades and finding the minimum grade, the maximum grade, and the average grade. Object-oriented design focuses on the data being manipulated. In this case, that data is 10 grades. We'll assume they're integer values for now. We can represent this as an array of ints in Java. So our class will be GradeCollection (I'm rethinking my original code a bit). These should be hidden within the object and not directly visble from the outside, so we declare the array private.
Ok, that doesn't do a thing. We need a constructor which sets up the initial state of our GradeCollection object. This constructor will need to know how many grades we want to be able to collect, since we're going to make this one flexible. Oh yeah, and we should probably make sure all of those grades are initialized to some default value we'll feed to the constructor.
Now we can create a new GradeCollection object, but it can't really do anything. We need to give it actions ("methods"). Firstly we'll probably want a way for people looking at this thing from the outside to figure out how many grades it can store. Since the array is private, though, we can't directly see it from outside of the black box that is our object.
Next we'll want a method which finds the minimum value in the array and shows it to us.
Now, I'll want another method, using almost exactly the same algorithm which finds the maximum grade.
Now, I need to find the average grade. Really, this is two issues. First I need to find the sum of all of the grades. This doesn't really need to be visible from outside, though, so let's make it private. The second part is to divide that number by the number of grades in the collection. For the greatest accuracy, let's make the average a floating point number.
Oh yeah, and we'll need a way to add a grade at a particular location, since our array of grades isn't accessible from outside.
Now we have everything necessary to create a main routine.
|
Author: | grasshopper [ Mon Apr 19, 2004 8:04 pm ] | ||
Post subject: | |||
wtd wrote: Yes. Teachers aren't always right.
![]()
heey... thanks sooo much.. our teacher FINALLY showed us how.. well actually she didn`t she gave us an assignment and told us we had to call them.. so i used that code you gave me.. great help!.. i know this is an old reply.. not sure you`ll even get this.. but thanks SOO mcuh ![]() |