Luninariel

app.java

Oct 29th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. package com.noynaert.csc254;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.Scanner;
  6.  
  7. /**
  8.  * Hello world!
  9.  *
  10.  */
  11. public class App
  12. {
  13.     public static int MAX = 100;
  14.  
  15.     public static void main( String[] args )
  16.     {
  17.         Rectangle[] boxes = new Rectangle[MAX];
  18.         int n = 0;
  19.  
  20.         n = readBoxes(boxes, "input.txt");
  21.  
  22.         print(boxes, n);
  23.  
  24.         System.out.println( "Programmed by YOUR NAME" );
  25.     }
  26.  
  27.     private static int readBoxes(Rectangle[] boxes, String fileName) {
  28.         int n = 0;
  29.         try {
  30.             Scanner input = new Scanner(new File(fileName));
  31.             while(input.hasNextLine() && n < MAX){
  32.                 double length, width;
  33.                 String line = input.nextLine();
  34.                 String[] parts = line.split("\\s+");
  35.  
  36.                 //Print the parts
  37.                 System.out.printf("%d parts found: ", parts.length);
  38.                 for(int i=0;i<parts.length;i++){
  39.                     System.out.printf("%f ", Double.parseDouble(parts[i]));
  40.                 }
  41.                 System.out.println();
  42.  
  43.                 //Create objects and put them in the array.
  44.                 //You may have to use all 3 of the constructors depending
  45.                 //    on the number of parts.  Use a SWITCH statement
  46.                 switch(parts.length){
  47.                     case 0:
  48.                         boxes[n] = new Rectangle();
  49.                         n++;
  50.                         break;
  51.                     case 1:
  52.                         boxes[n] = new Rectangle(Double.parseDouble(parts[0]));
  53.                         n++;
  54.                         break;
  55.                     case 2:
  56.                         boxes[n] = new Rectangle(Double.parseDouble(parts[0]),
  57.                                                  Double.parseDouble(parts[1]));
  58.                         n++;
  59.                         break;
  60.                     default:
  61.                         System.out.printf("Invalid line. Parts = %d\n",parts.length);
  62.                 }
  63.  
  64.  
  65.             }
  66.  
  67.             input.close();
  68.         }catch(FileNotFoundException e){
  69.             System.err.printf("File %s was not found\n",fileName);
  70.             System.exit(1);
  71.  
  72.         }
  73.  
  74.  
  75.         return n;
  76.     }
  77.     public static void print(Object[] things, int n){
  78.         System.out.println("\n---------------------");
  79.         for(int i=0;i<n;i++){
  80.             System.out.printf("[%3d]   %s\n",i,things[i]);
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment