-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f72427a
commit 8a9f059
Showing
9 changed files
with
368 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Rectangle> rectangleList; | ||
rectangleList = new ArrayList<>(); | ||
|
||
fillArrayList(rectangleList); | ||
printRectangles(rectangleList); | ||
|
||
}//end main | ||
|
||
public static void fillArrayList(ArrayList<Rectangle> 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<Rectangle> 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 | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Circle> circleList; | ||
circleList = new ArrayList<>(); | ||
|
||
fillArrayList(circleList); | ||
printCircles(circleList); | ||
|
||
}//end main | ||
|
||
private static void fillArrayList(ArrayList<Circle> 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<Circle> 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 | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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 | ||
} |