Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# JavaDataStructures
A repository for the Java programming projects I did in my Data Structures class.
A repository for the Java programming projects I did in my Data Structures class. {{Under contruction}} I need to delete all unneccesary test files/ clean up some of my code.

Binary file added lab1/ArrayList1.class
Binary file not shown.
130 changes: 130 additions & 0 deletions lab1/ArrayList1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// file name -- ArrayList1.java
import java.io.*;
import java.util.*;

/**
This program shows how to read in student records from an input
data fle and store them in an array list. Student records are
stored using an ArrayList object.
*/
public class ArrayList1
{
private ArrayList<Student> students;
private Scanner infile;

public static void main(String[] args)
throws IOException
{
ArrayList1 current;

current = new ArrayList1();
current.getStarted();
} // method main

private double gpa;
private double sumGPA;
private double averageGPA;
/**
Starts the program
*/
public void getStarted()
throws FileNotFoundException
{
openFile();
readData();
displayResults();
System.out.println("Average GPA:" + averageGPA);
infile.close();
} // method getStarted


/**
Opens the input data file for read access.
*/
public void openFile() throws FileNotFoundException
{
Scanner input;
String infile_name;
File file;

input = new Scanner(System.in);
do
{
System.out.print("Enter the input file name: ");
infile_name = input.next();

// open the input data file
file = new File(infile_name);
if (file.exists())
infile = new Scanner(new File(infile_name));
else
System.out.println(infile_name + " does not exist");
} while (!file.exists());
} // method openFile


/**
Reads in text from file and save them in an array list.
@param infile the file which has been open successfully
*/
public void readData()
{
Student who;
String name, first_name, last_name;
int age;

int count;

count = 0;
sumGPA = 0;

// instantiate an ArrayList object
students = new ArrayList<Student>();

// read in values from the file and assign them to the elements
while (infile.hasNext())
{
first_name = infile.next();
last_name = infile.next();
name = first_name + " " + last_name;
age = infile.nextInt();
gpa = infile.nextDouble();

count++;
sumGPA = sumGPA + gpa;


who = new Student(name, age, gpa);
students.add(who);
}
averageGPA = sumGPA / count;

} // method readData


/**
Displays the student records stored in the array list.
*/
public void displayResults()
{
// display the array elements in sequence
System.out.println("The list contains " + students.size() +
" students");
for (Student who: students)
displayStudentRecord(who);
} // method displayResults


/**
Displays the content of each student record.
@param who a reference to a Student object for display
*/
public void displayStudentRecord(Student who)
{

System.out.println("Name: " + who.getName());
System.out.println("Age: " + who.getAge());
System.out.println("GPA: " + who.getGPA());

} // method displayStudentRecord
} // class ArrayList1
Binary file added lab1/Student.class
Binary file not shown.
88 changes: 88 additions & 0 deletions lab1/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// file name -- Student.class

/**
A class to represent students objects.
Many of the functions are easy to see representations of getters and setters.
*/
public class Student
{
private String name;
private int age;
private double gpa;

/**
Initializes the members of a Student object.
@param name a string representing the name of the student
@param age an integer representing the age of the student
@param gpa a double value representing the gpa of the student
*/
public Student(String name,
int age,
double gpa)
{
this.name = name;
this.age = age;
this.gpa = gpa;
} // constructor


/**
Assigns a new name.
@param name a string representing the student's name
*/
public void setName(String name)
{
this.name = name;
} // method setName


/**
Assigns a new age.
@param age an integer representing the student's age
*/
public void setAge(int age)
{
this.age = age;
} // method setAge


/**
Assigns a new gpa.
@param age a double value representing the student's gpa
*/
public void setGPA(double gpa)
{
this.gpa = gpa;
} // method setGPA


/**
Returns the name of the student.
@return student's name
*/
public String getName()
{
return name;
} // method getName


/**
Returns the age of the student.
@return student's age
*/
public int getAge()
{
return age;
} // method getAge


/**
Returns the gpa of the student.
@return student's gpa
*/
public double getGPA()
{
return gpa;
} // method getGPA

} // class Student
5 changes: 5 additions & 0 deletions lab1/student.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
James Bond 48 3.25
Jim Smith 20 3.75
Jean Wilson 19 3.52
John Doe 22 2.5
Willie Fields 25 2.95
Binary file added lab2/FormattedInput02.class
Binary file not shown.
146 changes: 146 additions & 0 deletions lab2/FormattedInput02.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/**
file name -- FormattedInput02.java
This program shows how to read formatted data from a text file.

The data file contains student records which are formatted as follows:

Field name Columns (a.k.a. indexes)
==========================================
Name 1 - 20
Age 21 - 22
gpa 23 - 27
ssNumber 28 - 36
==========================================
*/


import java.io.*;
import java.util.*; // for Scanner class


class FormattedInput02
{
//final keyword makes these varibales, constant values, meaning you can't change them when aprogram is running.
//private keyword makes it so that these variables can only be accesed inside of the class.
private static final int NAME_WIDTH = 20;
private static final int AGE_WIDTH = 2;
private static final int GPA_WIDTH = 5;
private static final int SSNUMBER_WIDTH = 9;

private Scanner infile;
private ArrayList<Student> list;

public static void main(String list[]) throws IOException
{
FormattedInput02 one;

//makes a new instance of this class
one = new FormattedInput02();

one.getStarted();
} // method main


/**
Default Constructor
This makes creates a new Student type array list.
*/
public FormattedInput02()
{
list = new ArrayList<Student>();
} // constructor


/**
Starts the program using an object of the class
*/
public void getStarted() throws FileNotFoundException
{
openFile();
readData();
display();
} // method getStarted


/**
Opens the input data file for read access.
*/
public void openFile() throws FileNotFoundException
{
Scanner input;
String infile_name;
File file;

//Scanner will look for keyboard input.
input = new Scanner(System.in);

do
{
System.out.print("Enter the input file name: ");
//gets files name. stops looking at keybaord when user presses ENTER
infile_name = input.next();

file = new File(infile_name);
if (file.exists())
infile = new Scanner(file);
else
System.out.println(infile_name + " does not exist");
} while (!file.exists());
} // method openFile


/**
Reads student records from the file and separate the fields of
each record using substring method.
*/
public void readData()
{
String name, text;
int age;
double gpa;
String ssNumber;
Student who;

while (infile.hasNextLine())
{
text = infile.nextLine();

// obtain name field
name = text.substring(0, NAME_WIDTH); // extract name field
name = name.trim(); // remove leading and trailing spaces
//definetly need to use trim here, as we captured the name and all of the space behind them name and placed
//that into the name variable.

// remove the whole name field from text. Now substring index can start at 0 again for other variables.
text = text.substring(NAME_WIDTH, text.length());

// extract age field from text and convert it to an int type
age = Integer.parseInt(text.substring(0, AGE_WIDTH));

// extract gpa field from text and convert it to a double type. We place AGE_WIDTH into the first parameter
//as gpa is after it.
gpa = Double.parseDouble(text.substring(AGE_WIDTH, AGE_WIDTH+GPA_WIDTH)); //AGE_WIDTH + GPA_WIDTH

ssNumber = text.substring(AGE_WIDTH+GPA_WIDTH,AGE_WIDTH+GPA_WIDTH+SSNUMBER_WIDTH);


who = new Student(name, age, gpa, ssNumber);
//socialSecurity

list.add(who);
} // while infile still has data
} // method readData


public void display()
{
for (Student who : list)
{
System.out.println("Name: " + who.getName());
System.out.println("Age: " + who.getAge());
System.out.println("GPA: " + who.getGPA());
System.out.println("Social Security:" + who.getssNumber());
} // for still student records available
} // method display

} // class FormattedInput02
Binary file added lab2/Student.class
Binary file not shown.
Loading