/home/karlrees/public_html/gallery2/bla
/home/karlrees/public_html/gallery2/bla
/home/karlrees/public_html/gallery2/bla Java Lesson 2d | Wayne and Rebecca Madsen

Java Lesson 2d

wayne's picture

I have been working on this program for several days, attempting to start from scratch and write the entire program myself by using what I have already learned from the previous work in file input-output (IO). For this program, instead of copying a tutorial, I determined the design of my program before I began to code and set it within the linguistic parameters of what I knew, which at this point was only IO. We got rid of the textbook and have been tailoring my lessons more closely with what it is that I want to learn. 3D design does not lead me down the path of information analysis and so for the time being, I have put that behind me. Which obviously means that I do not remember anything of how to code 3D images. I am amazed at how quickly information escapes me. I could throw out a trite explanation of how our brains are muscles and we must exercise them to maintain their fitness, but I won't.

My grasp on the language has improved, but doesn't reach beyond a first year student. I am almost completely illiterate, but I am beginning to be able to comprehend the uses of the Java API and how to appropriately use it. This step into the grammar database of Java marks a step into experimentation with the vocabulary I have gained.

package inputOutput;

import java.util.*;
import java.io.*;

public class TestingGroups {
//global variables, only so i don't have to instantiate them into the doGroups method
static int value, groups = 0;
static Scanner myScan = new Scanner(System.in);

//method for running the numbers to the text file
public static void doGroups(File p_myFile) throws IOException{
//identify the number of times to run the while loop
int students, limit = 1;
System.out.println("How many students are in this group?: ");
students = myScan.nextInt();

//appendable buffered writing, so we can continue to build our stats
FileWriter fstream = new FileWriter (p_myFile, true);
BufferedWriter myBuffer = new BufferedWriter(fstream);

//print how many students per this group
myBuffer.write(" " + students);

//loop to get all the students' scores
while (limit <= students){
System.out.print("Enter student "+ limit + "'s score: ");
value = myScan.nextInt();
myBuffer.write("\t" + value);
limit++;
}

//formatting so we can open this file in excel later
myBuffer.write("\n");
myBuffer.close();
}

public static void main (String[] args)throws IOException{
//access and open file
String myString = new String();
Scanner myScan = new Scanner (System.in);
System.out.println("What file do you want to use?: ");
myString = myScan.nextLine().trim();
File myFile = new File(myString + ".txt");

//determine how many groups
while (!myString.equals("y")) {
System.out.println("How many groups are you working with today?: ");
System.out.println("[Hint: enter zero to see current scores]");
groups = myScan.nextInt();
System.out.println("You have decided to work with " + groups + " groups today. Is this correct? (y/n)");
myString = myScan.next().trim();
}

//run data entry
for (int i = 1; i <= groups; i++){
System.out.print("For group " + i +": ");
doGroups(myFile);
}

//reading the external text file
Scanner fileScan = new Scanner (myFile);
int groupcount = 1;

//loop for determining the absolute average values of all separate groups
while(fileScan.hasNextInt()){
groups = fileScan.nextInt();
int count = 0;
int average = 0;
for (int l=1; l <= groups; l++){
value = fileScan.nextInt();
count += value;
}
average = count/groups;
System.out.println("The average score for group number " + groupcount + " is " + average);
groupcount ++;
}

}

}