----------------------------------- grasshopper Mon Mar 29, 2004 4:38 pm 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 ----------------------------------- Tony Mon Mar 29, 2004 5:08 pm ----------------------------------- well first you'll need a function to compile your line of astrics, similar to turing's repeat string repeat(int size) { String word = new String; word = ""; for (int i=0;ito print the dots, use System.out.print(".") inside a loop body - ->might need the following code String inputString; Int times; .... Times= inputstring.length() //times will hold the number of characters in inputString 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..) //Variable Declaration String usersName; //the user's name String inputString; int times; //will hold the number of characters in inputString int mark; //Variable Initialization and Input System.out.print("Please enter your name: "); //prompt for input usersName = TextIO.getln(); //read from keyboard System.out.println("Please enter a mark: "); mark = TextIO.getlnInt(); //Program Processing and Output // the process and output i don`t get :cry: :cry: help? ----------------------------------- Dan Mon Apr 05, 2004 4:28 pm ----------------------------------- 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: int length = usersName.length() + 11; if(mark >= 10) { length = usersName.length() + 12; } else if(mark == 100) { length = usersName.length() + 13; } System.out.print(usersName + "'s mark is"); for (int i = 0; i < 30-length; i++) { System.out.print("."); } System.out.print(mark + "\n"); in this part: int length = usersName.length() + 11; if(mark >= 10) { length = usersName.length() + 12; } else if(mark == 100) { length = usersName.length() + 13; } 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. ----------------------------------- wtd Mon Apr 05, 2004 9:27 pm ----------------------------------- 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. String getFormattedString(String name, int grade) { } 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). String getFormattedString(String name, int grade) { int nameLen = name.length(); int gradeLen; if (grade < 10) gradeLen = 1; else if (grade < 100) gradeLen = 2; else gradeLen = 3; } 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. String getFormattedString(String name, int grade) { int nameLen = name.length(); int gradeLen; if (grade < 10) gradeLen = 1; else if (grade < 100) gradeLen = 2; else gradeLen = 3; int numOfDots = 30 - nameLen - gradeLen; } 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. String getFormattedString(String name, int grade) { int nameLen = name.length(); int gradeLen; if (grade < 10) gradeLen = 1; else if (grade < 100) gradeLen = 2; else gradeLen = 3; int numOfDots = 30 - nameLen - gradeLen; String dots = ""; for (int counter = 1; counter grades[i]) min = grades[i]; return min; } public int maxGrade() { int max = grades[0]; for (int i = 1; i < numberOfGrades(); i++) if (max < grades[i]) max = grades[i]; return max; } } } 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. class Exercise { public class GradeCollection { private int [] grades; public GradeCollection(int desiredLength, int defaultGrade) { grades = new int[desiredLength]; for (int i = 0; i < desiredLength; i++) grades[i] = defaultGrade; } public int length() { return grades.length; } public int minGrade() { int min = grades[0]; for (int i = 1; i < numberOfGrades(); i++) if (min > grades[i]) min = grades[i]; return min; } public int maxGrade() { int max = grades[0]; for (int i = 1; i < numberOfGrades(); i++) if (max < grades[i]) max = grades[i]; return max; } private int sumOfGrades() { int sum = 0; for (int i = 1; i < numberOfGrades(); i++) sum = sum + grades[i]; return sum; } public double averageGrade() { return ((double)sumOfGrades()) / numberOfGrades(); } } } 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. class Exercise { public class GradeCollection { private int [] grades; public GradeCollection(int desiredLength, int defaultGrade) { grades = new int[desiredLength]; for (int i = 0; i < desiredLength; i++) grades[i] = defaultGrade; } public int length() { return grades.length; } public int minGrade() { int min = grades[0]; for (int i = 1; i < numberOfGrades(); i++) if (min > grades[i]) min = grades[i]; return min; } public int maxGrade() { int max = grades[0]; for (int i = 1; i < numberOfGrades(); i++) if (max < grades[i]) max = grades[i]; return max; } private int sumOfGrades() { int sum = 0; for (int i = 1; i < numberOfGrades(); i++) sum = sum + grades[i]; return sum; } public double averageGrade() { return ((double)sumOfGrades()) / numberOfGrades(); } public void addGrade(int position, int grade) { grades[position] = grade; } } } Now we have everything necessary to create a main routine. class Exercise { public class GradeCollection { private int [] grades; public GradeCollection(int desiredLength, int defaultGrade) { grades = new int[desiredLength]; for (int i = 0; i < desiredLength; i++) grades[i] = defaultGrade; } public int length() { return grades.length; } public int minGrade() { int min = grades[0]; for (int i = 1; i < numberOfGrades(); i++) if (min > grades[i]) min = grades[i]; return min; } public int maxGrade() { int max = grades[0]; for (int i = 1; i < numberOfGrades(); i++) if (max < grades[i]) max = grades[i]; return max; } private int sumOfGrades() { int sum = 0; for (int i = 1; i < numberOfGrades(); i++) sum = sum + grades[i]; return sum; } public double averageGrade() { return ((double)sumOfGrades()) / numberOfGrades(); } } public static void main(String[] args) { BufferedReader keyboard = new BufferedReader( new InputStreamReader(System.in)); GradeCollection grades = new GradeCollection(10, 0); for (int i = 0; i < grades.numberOfGrades(); i++) { System.out.print("Please input a grade: "); int inputGrade = Integer.parseInt(keyboard.readLine()); grades.addGrade(inputGrade, i); } System.out.println("Max Grade: " + grades.maxGrade()); System.out.println("Min Grade: " + grades.minGrade()); System.out.println("Avg Grade: " + grades.averageGrade()); } } ----------------------------------- grasshopper Mon Apr 19, 2004 8:04 pm ----------------------------------- Yes. Teachers aren't always right. ;-) class Exercise { public static void printStuff() { // code here } public static void main(String[] args) { printStuff(); } } 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 :)