From 8a9f059c8dad29636b215d9b3b8e1a7268f2f87c Mon Sep 17 00:00:00 2001 From: "John P. Baugh" Date: Wed, 26 Jan 2022 23:43:09 -0500 Subject: [PATCH] Add files via upload --- section8/Circle.java | 44 ++++++++++++++++++ section8/FileInputFun.java | 33 ++++++++++++++ section8/FileOutputFun.java | 18 ++++++++ section8/NamesAges.java | 35 ++++++++++++++ section8/Proj8_1_RectangleFile.java | 64 ++++++++++++++++++++++++++ section8/Proj8_2_CircleFile.java | 71 +++++++++++++++++++++++++++++ section8/Rectangle.java | 46 +++++++++++++++++++ section8/TwiceData.java | 30 ++++++++++++ section8/WriteNames.java | 27 +++++++++++ 9 files changed, 368 insertions(+) create mode 100644 section8/Circle.java create mode 100644 section8/FileInputFun.java create mode 100644 section8/FileOutputFun.java create mode 100644 section8/NamesAges.java create mode 100644 section8/Proj8_1_RectangleFile.java create mode 100644 section8/Proj8_2_CircleFile.java create mode 100644 section8/Rectangle.java create mode 100644 section8/TwiceData.java create mode 100644 section8/WriteNames.java diff --git a/section8/Circle.java b/section8/Circle.java new file mode 100644 index 0000000..fc6b1dd --- /dev/null +++ b/section8/Circle.java @@ -0,0 +1,44 @@ + +public class Circle { + private double radius; + + public Circle() { + this(1.0); + }//no arg ctor + + public Circle(double radius) { + this.radius = radius; + }//end ctor + + public double getRadius() { + return radius; + }//end getRadius + + public void setRadius(double radius) { + this.radius = radius; + }//end setRadius + + public double circumference() { + return 2 * Math.PI * radius; + }//end circumference + + public double area() { + return Math.PI * Math.pow(radius, 2); + }//end area +}//end Circle class + + + + + + + + + + + + + + + + diff --git a/section8/FileInputFun.java b/section8/FileInputFun.java new file mode 100644 index 0000000..f028acf --- /dev/null +++ b/section8/FileInputFun.java @@ -0,0 +1,33 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.util.InputMismatchException; +import java.util.Scanner; + +public class FileInputFun { + public static void main(String[] args) { + Scanner infile; + + + try { + infile = new Scanner(new File("input.txt")); + int input; + int sum = 0; + + while(infile.hasNext()) { + input = infile.nextInt(); + System.out.println(input); + sum += input; //sum = sum + input + }//end while + + System.out.println("Sum is " + sum); + infile.close(); + } + catch(FileNotFoundException ex) { + System.out.println("Can't find file!"); + System.out.println(ex.getMessage()); + }//end try-catch + catch(InputMismatchException ex) { + System.out.println("Error reading input!"); + } + }//end main +} diff --git a/section8/FileOutputFun.java b/section8/FileOutputFun.java new file mode 100644 index 0000000..bb7fbf8 --- /dev/null +++ b/section8/FileOutputFun.java @@ -0,0 +1,18 @@ +import java.io.FileNotFoundException; +import java.io.PrintWriter; + +public class FileOutputFun { + public static void main(String[] args) { + + try { + PrintWriter pw = new PrintWriter("output.txt"); + + pw.println("Hello there"); + pw.println("My name is John Baugh!"); + pw.close(); + } + catch(FileNotFoundException ex) { + System.out.println("Couldn't write to the file!"); + }//end try-catch + }//end main +} diff --git a/section8/NamesAges.java b/section8/NamesAges.java new file mode 100644 index 0000000..3ee5e7f --- /dev/null +++ b/section8/NamesAges.java @@ -0,0 +1,35 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintWriter; +import java.util.Scanner; + +public class NamesAges { + public static void main(String[] args) { + Scanner namesFile; + Scanner agesFile; + PrintWriter pw; + + try { + namesFile = new Scanner(new File("student_names.txt")); + agesFile = new Scanner(new File("student_ages.txt")); + pw = new PrintWriter("names_ages.txt"); + + String tempName; + int tempAge; + + while(namesFile.hasNext()) { + tempName = namesFile.nextLine(); + tempAge = agesFile.nextInt(); + + pw.println(tempName + " is " + tempAge + " years old."); + }//end while + + namesFile.close(); + agesFile.close(); + pw.close(); + } + catch(FileNotFoundException ex) { + System.out.println(ex.getMessage()); + }//end try-catch + }//end main +} diff --git a/section8/Proj8_1_RectangleFile.java b/section8/Proj8_1_RectangleFile.java new file mode 100644 index 0000000..b6c399f --- /dev/null +++ b/section8/Proj8_1_RectangleFile.java @@ -0,0 +1,64 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.Scanner; + +public class Proj8_1_RectangleFile { + public static void main(String[] args) { + ArrayList rectangleList; + rectangleList = new ArrayList<>(); + + fillArrayList(rectangleList); + printRectangles(rectangleList); + + }//end main + + public static void fillArrayList(ArrayList rectangleAL) { + Scanner infile; + + try { + infile = new Scanner(new File("rectangle_data.txt")); + Rectangle temp; + double length; + double width; + + while(infile.hasNext()) { + length = infile.nextDouble(); + width = infile.nextDouble(); + temp = new Rectangle(length, width); + rectangleAL.add(temp); + }//end while + + infile.close(); //close the file! + } + catch(FileNotFoundException ex) { + System.out.println("Error accessing file!"); + }//end try-catch + }//end fillArrayList + + public static void printRectangles(ArrayList rectangleAL) { + for(Rectangle r : rectangleAL) { + System.out.println("Length: " + r.getLength()); + System.out.println("Width: " + r.getWidth()); + System.out.println("Area: " + r.area()); + System.out.println("Perimeter: " + r.perimeter()); + System.out.println(); + }//end for + }//end printRectangles +} + + + + + + + + + + + + + + + + diff --git a/section8/Proj8_2_CircleFile.java b/section8/Proj8_2_CircleFile.java new file mode 100644 index 0000000..70f3916 --- /dev/null +++ b/section8/Proj8_2_CircleFile.java @@ -0,0 +1,71 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Scanner; + +public class Proj8_2_CircleFile { + public static void main(String[] args) { + ArrayList circleList; + circleList = new ArrayList<>(); + + fillArrayList(circleList); + printCircles(circleList); + + }//end main + + private static void fillArrayList(ArrayList circleAL) { + Scanner infile; + + try { + infile = new Scanner(new File("circle_data.txt")); + Circle temp; + double radius; + + while(infile.hasNext()) { + radius = infile.nextDouble(); + temp = new Circle(radius); + circleAL.add(temp); + }//end while + + infile.close(); + } + catch(FileNotFoundException ex) { + System.out.println("Error accessing file!"); + }//end try-catch + }//end fillArrayList + + private static void printCircles(ArrayList circleAL) { + PrintWriter pw; + + try { + pw = new PrintWriter("circles_output.txt"); + for(Circle c : circleAL) { + System.out.println("Radius: " + c.getRadius()); + System.out.println("Circumference: " + c.circumference()); + System.out.println("Area: " + c.area()); + System.out.println(); + + pw.println("Radius: " + c.getRadius()); + pw.println("Circumference: " + c.circumference()); + pw.println("Area: " + c.area()); + pw.println(); + }//end for + + pw.close(); + } + catch(FileNotFoundException ex) { + System.out.println("Couldn't access file."); + }//end try-catch + }//end printCircles +} + + + + + + + + + + diff --git a/section8/Rectangle.java b/section8/Rectangle.java new file mode 100644 index 0000000..e8abd1a --- /dev/null +++ b/section8/Rectangle.java @@ -0,0 +1,46 @@ + +public class Rectangle { + private double length; + private double width; + + private static int numRectangles; + + public Rectangle() { + this(1.0, 1.0); + }//end no-arg ctor + + public Rectangle(double length, double width) { + this.length = length; + this.width = width; + numRectangles++; + }//end ctor + + public double getLength() { + return length; + } + + public double getWidth() { + return width; + } + + public void setLength(double length) { + this.length = length; + } + + public void setWidth(double width) { + this.width = width; + } + + public double area() { + return length * width; + } + + public double perimeter() { + return 2 * length + 2 * width; + } + + public static int getNumRectangles() { + return numRectangles; + } + +} diff --git a/section8/TwiceData.java b/section8/TwiceData.java new file mode 100644 index 0000000..ada9845 --- /dev/null +++ b/section8/TwiceData.java @@ -0,0 +1,30 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintWriter; +import java.util.Scanner; + +public class TwiceData { + public static void main(String[] args) { + Scanner infile; + PrintWriter pw; + + try { + infile = new Scanner(new File("nums.txt")); + pw = new PrintWriter("twice_nums.txt"); + + int inputNum; + + while(infile.hasNext()) { + inputNum = infile.nextInt(); + inputNum *= 2; //multiply by 2 + pw.println(inputNum); + }//end while + + infile.close(); + pw.close(); + } + catch(FileNotFoundException ex) { + System.out.println(ex.getMessage()); + }//end try-catch + }//end main +} diff --git a/section8/WriteNames.java b/section8/WriteNames.java new file mode 100644 index 0000000..67a7809 --- /dev/null +++ b/section8/WriteNames.java @@ -0,0 +1,27 @@ +import java.io.FileNotFoundException; +import java.io.PrintWriter; +import java.util.ArrayList; + +public class WriteNames { + public static void main(String[] args) { + ArrayList names = new ArrayList<>(); + + names.add("Samantha"); + names.add("John"); + names.add("Kyle"); + names.add("Heather"); + names.add("Luke"); + + try { + PrintWriter pw = new PrintWriter("names.txt"); + for(String name : names) { + pw.println(name); + }//end for + + pw.close(); + } + catch(FileNotFoundException ex) { + System.out.println("Can't write to file!"); + } + }//end main +}