We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

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

15jen
15jen asked
on
Medium Priority
367 Views
Last Modified: 2013-12-29
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;
     
    }

}
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
When you post code, could you please post it in the snippet window?

Author

Commented:
Ok
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
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;

            }

      }
}


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.