[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

How do I code numgen.print_chart to display comparison results?

Here is my code, I am trying to add a method that prints out a chart of the swaps in the sort methods.  I started to put the numgen.chart_numbers code in but am stuck.  

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.*;
import java.io.*;

public class Main2 {
 
  public static void main ( String[] args ) throws IOException {
    Scanner scan = new Scanner ( System.in );
    int selection = 0;
    System.out.println("CS501 Howework #1 - Jennifer Pelletier");
    System.out.println("Input the amount of Numbers you want to sort:");
    int elements = scan.nextInt();
    scan.nextLine();
    Number numgen = new Number();
    numgen.generate(elements);
    numgen.show_numbers(elements);
    numgen.put_numbers(elements);
    numgen.chart_numbers(elements);
    System.out.println("Select the Sort Type?");
    System.out.println("1) Selection Sort");
    System.out.println("2) Bubble Sort");
    System.out.println("3) Insertion Sort");
    int swaps = 0;

    switch ( scan.nextInt() ) {
        case 1:
        selection = 1;
        numgen.selectionsort(elements);
        numgen.show_numbers(elements);
      break;
          case 2:
          selection = 2;
        numgen.bubblesort(elements);
          numgen.show_numbers(elements);
        break;       
            case 3:
            selection = 3;
          numgen.insertionsort(elements);
            numgen.show_numbers(elements);
          break;
            default:
            System.out.println("Error");
          break;
  }
    numgen.put_numbers(elements);
    System.out.println("View Results Chart?(Y/N):");
    numgen.get_numbers(elements);
    System.out.print("Enter Choice:");
    switch ( scan.nextInt() ) {
       case 4:
       selection = Y;
       numgen.chart_numbers(elements);
       break;
        
   
}
   class Number{
      int[] randomnumbers;
      public void generate(int elements){
        int min = 1;
        int max = 2000;
        randomnumbers = new int[elements];
      for (int i = 0; i < elements; i++) {
           randomnumbers[i] = (int) (Math.random() * (max - min + 1) ) + min;
               
        }
     }
 

    public void put_numbers(int elements) throws IOException{
          try{
              PrintWriter out = new PrintWriter(new FileWriter("random.txt", true));
            for (int j = 0; j < randomnumbers.length; j++){      
                      out.println(randomnumbers[j]);
      }
      out.close();
      }
      catch (IOException e){
      e.printStackTrace();
}
   }

      public void get_numbers(elements){
      int elements = 0;
try{
      BufferedReader input = new BufferedReader(new FileReader("random.txt"));
      String str;
      while ((str = input.readLine()) !=null){
      elements++;
      }
     
      randomnumbers = new int[elements];
      for (int f=0; f < elements; f++){
       Scanner scanin = new Scanner(System.in);
       String stringnum = scanin.next();
       char[] chars = stringnum.toCharArray();
       int[] intnum = new int[chars.length];
       for (int i=0; i < chars.length; ++i){
           intnum[i] = Integer.parseInt(String.valueOf(chars[i]));
      }
      System.arraycopy(intnum, 0, randomnumbers, 0, Math.min(
            intnum.length, randomnumbers.length));
      //randomnumbers[f] = intnum[i];
      }
      input.close();
     }catch (IOException e) {
     }
  }      
   
      public void show_numbers(int elements){
        for (int j = 0; j < elements; j++){
          System.out.println(randomnumbers[j]);
                     
    }
}
      public int selectionsort(int elements) throws IOException {
           int outside, inside, min, swaps = 0;
           for(outside=0; outside < elements - 1; outside++){
             min = outside;
               for(inside=outside+1; inside < elements; inside++)
               if (randomnumbers[inside] < randomnumbers[min])
                       min = inside;
                        swap(outside, min);
                  swaps++;
                   }
                 return swaps;

  }
     public int bubblesort(int elements){
      int outside, inside, swaps = 0;
      for(outside=elements-1; outside > 1; outside--)
          for (inside=0; inside < outside; inside++)
          if(randomnumbers[inside] > randomnumbers[inside+1]){
             swap(inside, inside+1);
             swaps++;
          }
       return swaps;
    }
      
      public int insertionsort(int elements){
        int inside, outside, swaps = 0;
      for(outside=1; outside < elements; outside++){
         int temp = randomnumbers[outside];
         inside = outside;
         while((inside>0) && (randomnumbers[inside-1] >= temp)){
             randomnumbers[inside] = randomnumbers[inside-1];
               --inside;
             swaps++;
         }
      randomnumbers[inside] = temp;
     }      
     return swaps;
   }
         
         public void swap(int one, int two){
           int temp = randomnumbers[one];
           randomnumbers[one] = randomnumbers[two];
           randomnumbers[two] = temp;
     
    }

}
0
15jen
Asked:
15jen
1 Solution
 
CEHJCommented:
When you post code, could you please post it in the snippet window?
0
 
15jenAuthor Commented:
Ok
0
 
objectsCommented:
try this:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.*;
import java.io.*;

public class Main2 {

      public static void main(String[] args) throws IOException {
            Scanner scan = new Scanner(System.in);
            int selection = 0;
            System.out.println("CS501 Howework #1 - Jennifer Pelletier");
            System.out.println("Input the amount of Numbers you want to sort:");
            int elements = scan.nextInt();
            scan.nextLine();
            Number numgen = new Number();
            numgen.generate(elements);
            numgen.show_numbers(elements);
            numgen.put_numbers(elements);
            numgen.chart_numbers();
            System.out.println("Select the Sort Type?");
            System.out.println("1) Selection Sort");
            System.out.println("2) Bubble Sort");
            System.out.println("3) Insertion Sort");
            int swaps = 0;

            switch (scan.nextInt()) {
            case 1:
                  selection = 1;
                  numgen.selectionsort(elements);
                  numgen.show_numbers(elements);
                  break;
            case 2:
                  selection = 2;
                  numgen.bubblesort(elements);
                  numgen.show_numbers(elements);
                  break;
            case 3:
                  selection = 3;
                  numgen.insertionsort(elements);
                  numgen.show_numbers(elements);
                  break;
            default:
                  System.out.println("Error");
                  break;
            }
            numgen.put_numbers(elements);
            System.out.println("View Results Chart?(Y/N):");
            numgen.get_numbers();
            System.out.print("Enter Choice:");
            switch (scan.nextInt()) {
            case 4:
                  // selection = Y;
                  numgen.chart_numbers();
                  break;
            }

      }

      static class Number {
            int[] randomnumbers;

            public void generate(int elements) {
                  int min = 1;
                  int max = 2000;
                  randomnumbers = new int[elements];
                  for (int i = 0; i < elements; i++) {
                        randomnumbers[i] = (int) (Math.random() * (max - min + 1))
                                    + min;

                  }
            }

            public void put_numbers(int elements) throws IOException {
                  try {
                        PrintWriter out = new PrintWriter(new FileWriter("random.txt",
                                    true));
                        for (int j = 0; j < randomnumbers.length; j++) {
                              out.println(randomnumbers[j]);
                        }
                        out.close();
                  } catch (IOException e) {
                        e.printStackTrace();
                  }
            }

            public void chart_numbers() {
            }

            public void get_numbers() {
                  int elements = 0;
                  try {
                        BufferedReader input = new BufferedReader(new FileReader(
                                    "random.txt"));
                        String str;
                        while ((str = input.readLine()) != null) {
                              elements++;
                        }

                        randomnumbers = new int[elements];
                        for (int f = 0; f < elements; f++) {
                              Scanner scanin = new Scanner(System.in);
                              String stringnum = scanin.next();
                              char[] chars = stringnum.toCharArray();
                              int[] intnum = new int[chars.length];
                              for (int i = 0; i < chars.length; ++i) {
                                    intnum[i] = Integer.parseInt(String.valueOf(chars[i]));
                              }
                              System.arraycopy(intnum, 0, randomnumbers, 0, Math.min(
                                          intnum.length, randomnumbers.length));
                              // randomnumbers[f] = intnum[i];
                        }
                        input.close();
                  } catch (IOException e) {
                  }
            }

            public void show_numbers(int elements) {
                  for (int j = 0; j < elements; j++) {
                        System.out.println(randomnumbers[j]);

                  }
            }

            public int selectionsort(int elements) throws IOException {
                  int outside, inside, min, swaps = 0;
                  for (outside = 0; outside < elements - 1; outside++) {
                        min = outside;
                        for (inside = outside + 1; inside < elements; inside++)
                              if (randomnumbers[inside] < randomnumbers[min])
                                    min = inside;
                        swap(outside, min);
                        swaps++;
                  }
                  return swaps;

            }

            public int bubblesort(int elements) {
                  int outside, inside, swaps = 0;
                  for (outside = elements - 1; outside > 1; outside--)
                        for (inside = 0; inside < outside; inside++)
                              if (randomnumbers[inside] > randomnumbers[inside + 1]) {
                                    swap(inside, inside + 1);
                                    swaps++;
                              }
                  return swaps;
            }

            public int insertionsort(int elements) {
                  int inside, outside, swaps = 0;
                  for (outside = 1; outside < elements; outside++) {
                        int temp = randomnumbers[outside];
                        inside = outside;
                        while ((inside > 0) && (randomnumbers[inside - 1] >= temp)) {
                              randomnumbers[inside] = randomnumbers[inside - 1];
                              --inside;
                              swaps++;
                        }
                        randomnumbers[inside] = temp;
                  }
                  return swaps;
            }

            public void swap(int one, int two) {
                  int temp = randomnumbers[one];
                  randomnumbers[one] = randomnumbers[two];
                  randomnumbers[two] = temp;

            }

      }
}


0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now