Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that generates interesting numerical data and uses Java gra

ID: 3820242 • Letter: W

Question

Write a Java program that generates interesting numerical data and uses Java graphics to visualize the data. Required elements include: Use methods containing loops to generate data from two of the following mathematical description and write the data to files (each sequence is a separate method). The first (Arithmetic sequence) is required; you may choose either of the other two: An Arithmetic sequence is defined as the set of numbers formed by adding a constant value to each successive term beginning with the first term, which is 1. so, for example, if the constant is 5, the first several numbers in the series are: 1, 6, 11, 16, 21, 26, 31, 36 The Square series is the set of n terms where each term is the square of its position, starting from 1. So, for n=12 the series is: 1, 4, 9, 16, 25, 49, 64, 81, 100, 121, 144 The Fibonacci series is defined as follows: the first number in the series is 1, the second is 2 - after that, each number is the sum of the two previous numbers. The first several values are thus: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 Using the data files you generated, as input sources, write a Java program that uses graph.es to visualize the series using either ASCII art or swing objects. Each visualization is a single method. Examples of some (not very impressive) visualizations are shown below: Fibonacci numbers (2 different representations) Your main method should: call the necessary methods to generate the data files (do this just once per program run) display a menu of visualizations the user can look at display the user's choice of visualizations cycle through steps 2 and 3 as long as the user wishes Sample programs (that deal with random numbers, not deliberate series) are provided with this assignment - take a look for hints on how to cycle between different visualizations Extra credit option: Come up with your own interesting number series and create a visualization. Your series cannot be random - it must have a mathematical basis, and that must be documented in the code that generates the data.

Explanation / Answer

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.*;
import java.io.*;


class GraphDraw extends JComponent
{
   int[] data;
   public GraphDraw(int[] data) {
       this.data = data;
   }
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;

for (int i=0; i<data.length; i++) {
Ellipse2D circle2D = new Ellipse2D.Double();
           g2.setPaint(Color.RED);
circle2D.setFrameFromCenter(data[i], data[i], 2 * data[i], 2 * data[i]);
g2.draw(circle2D);
          
}
}
  
}

class AddFrame extends JFrame
{
   public AddFrame(int option, int series_type)
   {
       setTitle("Visualization");
      

       int[] data = new int[1];
              
       String file_name = "" ;
       if(series_type == 1) { // arithmetic
           file_name = "arithmetic.txt";
       } else if(series_type == 2) { // square
           file_name = "square.txt";
       } else if(series_type == 3) { // fibonaci
           file_name = "fibonaci.txt";
       }
  
       try{
           FileInputStream fstream = new FileInputStream(file_name);
           DataInputStream in = new DataInputStream(fstream);
           BufferedReader br = new BufferedReader(new InputStreamReader(in));
           String strLine;
           while ((strLine = br.readLine()) != null) {
               String[] tokens = strLine.split(" ");
               data = new int[tokens.length];
               for(int i=0; i<tokens.length; i++) {
                   data[i] = Integer.parseInt(tokens[i]);
               }
           }
           in.close();
          
           switch (option) {
               case 1 :
                   setSize(500, 500);
                   GraphDraw component = new GraphDraw(data);
                   add(component);
                   break;
               case 2 :
                   setSize(1000, 1000);
                   JPanel mainPanel = new JPanel();
                   mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
                   for(int i=0; i<data.length; i++) {
                       JPanel topPanel = new JPanel();
                       for(int j=1; j<=data[i]; j++) {
                           JLabel lab1 = new JLabel("$ ", JLabel.LEFT);
                           setLayout(new FlowLayout());
                           topPanel.add(lab1);
                       }
                       mainPanel.add(topPanel, BorderLayout.SOUTH);
                   }
                  
                   add(mainPanel);
           }
       }catch (Exception e){
           System.err.println("Error: " + e.getMessage());
       }
  
   }

}

public class series {
  
   static int n = 10;
   static int[] arithmeticEQ, square, fibonaci;
  
   void writeDataToFile(int[] data, int file_type) {
      
       String data_str = "";
      
       data_str = data[0] + "";
       for(int i=1; i<n; i++) {
           data_str += " " + data[i];
       }
      
       String file_name = "" ;
       if(file_type == 1) { // arithmetic
           file_name = "arithmetic.txt";
       } else if(file_type == 2) { // square
           file_name = "square.txt";
       } else if(file_type == 3) { // fibonaci
           file_name = "fibonaci.txt";
       }
      
      
       try {
PrintWriter out = new PrintWriter(new FileWriter(file_name));
           out.println(data_str);
out.close();
} catch (IOException e) {
System.out.print("Error: " + e);
System.exit(1);
}
   }
  
   int[] arithmaticSequence() {
       int[] data = new int[n];
       data[0] = 1;
       for(int i=1; i<n; i++) {
           data[i] = data[i-1] + 5;
       }
       return data;
   }
  
   int[] squareSeries() {
       int[] data = new int[n];
       data[0] = 1;
       int interval = 3;
       for(int i=1; i<n; i++) {
           data[i] = data[i-1] + interval;
           interval += 2;
       }
       return data;
   }
  
   int[] fibonaciSeries() {
       int[] data = new int[n];
       data[0] = 1;
       data[1] = 2;
       for(int i=2; i<n; i++) {
           data[i] = data[i-2] + data[i-1];
       }
       return data;
   }
  
   public static void main(String args[]) {
  
       series ser = new series();
       arithmeticEQ = ser.arithmaticSequence();
  
       System.out.println("------------------------------");
       System.out.println("Arithmetic Sequence");
       for(int i=0; i<n; i++) {
           System.out.print(arithmeticEQ[i] + " " ) ;
       }
       ser.writeDataToFile(arithmeticEQ, 1);
      
       square = ser.squareSeries();
  
       System.out.println(" ------------------------------");
       System.out.println("Square Series");
       for(int i=0; i<n; i++) {
           System.out.print(square[i] + " " ) ;
       }
       ser.writeDataToFile(square, 2);
      
       fibonaci = ser.fibonaciSeries();
  
       System.out.println(" ------------------------------");
       System.out.println("Fibonaci Series");
       for(int i=0; i<n; i++) {
           System.out.print(fibonaci[i] + " " ) ;
       }
       ser.writeDataToFile(fibonaci, 3);
  
       int option = 0;
      
       while(option != 3) {
      
           Scanner reader = new Scanner(System.in); // Reading from System.in
          
           System.out.println(" ---------------------------");
           System.out.println("1 : Visual 1 ");
           System.out.println("2 : Visual 2 ");
           System.out.println("3 : Exit ");
           System.out.print("Choose from menu : ");
           option = reader.nextInt();
          
          
           if(option != 3) {
          
               System.out.println("---------------------------");
               System.out.println("1 : Arithmetic Sequence ");
               System.out.println("2 : Square Series ");
               System.out.println("3 : Fibonaci Series ");
               System.out.print("Choose from menu : ");
               int series_type = reader.nextInt();
          
               AddFrame frame = new AddFrame(option, series_type);
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.setVisible(true);
           }
          
       }
      
   }
}

/* output ::

------------------------------
Arithmetic Sequence
1 6 11 16 21 26 31 36 41 46

------------------------------
Square Series
1 4 9 16 25 36 49 64 81 100

------------------------------
Fibonaci Series
1 2 3 5 8 13 21 34 55 89

---------------------------
1 : Visual 1
2 : Visual 2
3 : Exit
Choose from menu : 2
---------------------------
1 : Arithmetic Sequence
2 : Square Series
3 : Fibonaci Series
Choose from menu : 1 */

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote