Second programming assignment
#1
Over spring break we where given a programming assignment. After my first one I got marked off a few points for repeat code that was not needed.
The assignment: https://imgur.com/a/rzI0N
In the instructions we cant use arrays
so heres my code for the assignment
Code:
import java.io.*;
import java.util.*;

public class Prog02{
public static void main (String [] args) throws IOException{

//stuff
Scanner kb = new Scanner(System.in);
Random rand = new Random();
final int GAUSS_STDEV = 10;
final int GAUSS_MEAN = 75;
int amountOfRandom;
int randomNumbers;
int gaussNums;
int range;
int gRange;
int count = 0;
double gaussMean = 0;
int gaussMin = 100;
int gaussMax = 0;
int min = 100;
int max = 0;
double mean = 0;
double standardDeviation = 0;
double gStandardDeviation = 0;

File file = new File("regNums.txt");
PrintWriter outputFile = new PrintWriter(file);
  File inFile = new File("gaussNums.txt");
  PrintWriter outFile = new PrintWriter(inFile);
  File barFile = new File("barChartFile.txt");
  PrintWriter barOut = new PrintWriter(barFile);


System.out.print("How many random numbers (0...100) should i generate: ");
amountOfRandom = kb.nextInt();
System.out.println();

while (count<amountOfRandom){ //Generates random numbers for regNums.txt
randomNumbers = rand.nextInt(100);
gaussNums = (int)(rand.nextGaussian()* GAUSS_STDEV + GAUSS_MEAN);
outputFile.println(randomNumbers);
outFile.println(gaussNums);
count++;
}
outputFile.close();
outFile.close();

Scanner inputFile = new Scanner(file);
Scanner iFile = new Scanner(inFile);
while (inputFile.hasNext()){
int number = inputFile.nextInt();
int gaussNumber = iFile.nextInt();
mean = mean + number;
gaussMean = gaussMean + gaussNumber;
if (number < min){
min = number;
}
if (number > max){
max = number;
}
if (gaussNumber < gaussMin){
gaussMin = gaussNumber;
}
if (gaussNumber > gaussMax){
gaussMax = gaussNumber;
}
}
mean = mean/amountOfRandom;
gaussMean = gaussMean/amountOfRandom;
inputFile.close();

Scanner standardD = new Scanner(file); //Having to re open the file one more time for standard deviation
Scanner standardD2 = new Scanner(inFile);
while (standardD.hasNext()){
int number = standardD.nextInt();
int gaussNumber = standardD2.nextInt();
standardDeviation += Math.pow(number - mean, 2);
gStandardDeviation += Math.pow(gaussNumber - gaussMean, 2);
}
gStandardDeviation = Math.sqrt(gStandardDeviation/amountOfRandom);
standardDeviation = Math.sqrt(standardDeviation/amountOfRandom);
range = (max - min) + 1;
gRange = (gaussMax - gaussMin) + 1;
System.out.println("Measures for the normally distributed integers in file, regNums.txt:");
  System.out.println("Min: " + min);
  System.out.println("Max: " + max);
  System.out.println("Range: " + range);
  System.out.println("Mean: " + mean);
  System.out.println("StDev: " + standardDeviation);
  System.out.println();

  System.out.println("Bar chart for the normally distributed integers in file, gaussNums.txt (50...100): ");

  count = 49;
  while (count<100){
  count++;
  System.out.println();
  barOut.println();
  System.out.print(count + ":");
  barOut.print( count + ":");
  Scanner input2 = new Scanner(inFile);
  while (input2.hasNext()){
  int gaussNumber = input2.nextInt();
  if (gaussNumber == count){
  System.out.print("*");
  barOut.print("*");
  }
  }
  }
  barOut.close();
  System.out.println();
  System.out.println();
System.out.println("Measures for the normally distributed integers in file, gaussNums.txt:");
  System.out.println("Min: " + gaussMin);
  System.out.println("Max: " + gaussMax);
  System.out.println("Range: " + gRange);
  System.out.println("Mean: " + gaussMean);
  System.out.println("StDev: " + gStandardDeviation);
  System.out.println();  
}
}
what are your thoughts?
Reply
#2
For your second assignment this is actually pretty good, understanding of files, iteration... not bad  Tongue
Your indentation looks fucked tho, was the copy+pasting or do you actually program like that?
Reply
#3
your code seems to be very procedural. if you havent learned oop yet then i can understand but i dont think this is considered to be "clean" java code.
Reply
#4
(03-14-2018, 09:14 AM)FeNRiR Wrote: your code seems to be very procedural. if you havent learned oop yet then i can understand but i dont think this is considered to be "clean" java code.
in the requirements we have to use what we learned in class. Sadly if I go ahead and use arrays, classes, or methods etc i get marked off a lot of points. but yeah i understand what you mean just trying not to have any points taken off

(03-14-2018, 09:01 AM)enmafia2 Wrote: For your second assignment this is actually pretty good, understanding of files, iteration... not bad  Tongue
Your indentation looks fucked tho, was the copy+pasting or do you actually program like that?

I believe it was the copy and pasting cause it looks clean in sublime 3. And thanks man! I plan on posting a few of my assignments I do so I can see my progression.
Reply
#5
(03-14-2018, 06:16 PM)ImArtisan Wrote:
(03-14-2018, 09:14 AM)FeNRiR Wrote: your code seems to be very procedural. if you havent learned oop yet then i can understand but i dont think this is considered to be "clean" java code.
in the requirements we have to use what we learned in class. Sadly if I go ahead and use arrays, classes, or methods etc i get marked off a lot of points. but yeah i understand what you mean just trying not to have any points taken off

(03-14-2018, 09:01 AM)enmafia2 Wrote: For your second assignment this is actually pretty good, understanding of files, iteration... not bad  Tongue
Your indentation looks fucked tho, was the copy+pasting or do you actually program like that?

I believe it was the copy and pasting cause it looks clean in sublime 3. And thanks man! I plan on posting a few of my assignments I do so I can see my progression.

I’m not sure if you’re familiar with GitHub but it is a great platform for sharing code. The community is great and it is very powerful and used in real scenarios.
Reply
#6
(03-14-2018, 06:16 PM)ImArtisan Wrote: in the requirements we have to use what we learned in class. Sadly if I go ahead and use arrays, classes, or methods etc i get marked off a lot of points. but yeah i understand what you mean just trying not to have any points taken off

thats real unfortunate. is there a reason for that? i remember when i did a c course and the lecturer only allowed us to use certain libraries but not limiting our use of the language itself so that we would develop problem solving skills.
Reply
#7
(03-15-2018, 12:56 AM)FeNRiR Wrote:
(03-14-2018, 06:16 PM)ImArtisan Wrote: in the requirements we have to use what we learned in class. Sadly if I go ahead and use arrays, classes, or methods etc i get marked off a lot of points. but yeah i understand what you mean just trying not to have any points taken off

thats real unfortunate. is there a reason for that? i remember when i did a c course and the lecturer only allowed us to use certain libraries but not limiting our use of the language itself so that we would develop problem solving skills.

Honestly not even sure why he does that. My biggest assumption is to hope that no one googles how to do the assignment. I do have a lot of problems with this teacher in my last program I used System.exit(0) twice. and he took 10 points off my assignment for using it. Saying we dont use it in this course when its in a chapter we already went over and it was on a power point he present in class. The dude doesnt even show his own code half the time every code he shows is straight from the book sadly at my community college hes the highest rated professor for this class with a 2.6/4.

(03-14-2018, 08:02 PM)enmafia2 Wrote:
(03-14-2018, 06:16 PM)ImArtisan Wrote:
(03-14-2018, 09:14 AM)FeNRiR Wrote: your code seems to be very procedural. if you havent learned oop yet then i can understand but i dont think this is considered to be "clean" java code.
in the requirements we have to use what we learned in class. Sadly if I go ahead and use arrays, classes, or methods etc i get marked off a lot of points. but yeah i understand what you mean just trying not to have any points taken off

(03-14-2018, 09:01 AM)enmafia2 Wrote: For your second assignment this is actually pretty good, understanding of files, iteration... not bad  Tongue
Your indentation looks fucked tho, was the copy+pasting or do you actually program like that?

I believe it was the copy and pasting cause it looks clean in sublime 3. And thanks man! I plan on posting a few of my assignments I do so I can see my progression.

I’m not sure if you’re familiar with GitHub but it is a great platform for sharing code. The community is great and it is very powerful and used in real scenarios.
Create my github now Smile
https://github.com/ImArtisann/JavaAssignment02/
Reply
#8
(03-15-2018, 06:01 AM)ImArtisan Wrote: Create my github now Smile
https://github.com/ImArtisann/JavaAssignment02/

Sounds shitty, they might do it so you really learn the basics. Some people don't get the basics well because there are functions that do all the work and when they go to lower level programming languages they are completely lost.
Anyways don't worry, you can always push you further at home Wink

There are thousands of hacking projects to do, just get inspired and start programming Tongue

P.S. welcome to github btw Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  First Attempt at Android Programming Psycho_Coder 4 24,332 01-07-2017, 07:06 AM
Last Post: Psycho_Coder