week4_src/ArrayMethods.javaweek4_src/ArrayMethods.javapackage edu.drexel.ct290;
publicclassArrayMethods{
/**
* Pass and array to a method that will search through it for a given value.
*
* @param elementToFind: This is the item we want to find in the array.
* @param stringArray: This is the array to search through.
* @return Returns: The index of the element, or -1 if not found.
*/
publicint findElement(String elementToFind,String[] stringArray ){
int index =-1;
for(int i=0; i<stringArray.length; i++){
if( elementToFind.equals(stringArray[i])){
// If the element is found, set index
index = i;
}
}
return index;
}
/**
* Replace a value in an array.
*
* @param indexToReplace: The index where the new value should be written.
* @param newValue: The new value.
* @param stringArray: Change a value in this array.
* @return True if the given index is valid
*/
publicboolean replace(int indexToReplace,String newValue,String[] stringArray ){
boolean replaced =false;
// Use the if condition to make sure the given index is valid
if( indexToReplace >0&& indexToReplace < stringArray.length ){
stringArray[indexToReplace]= newValue;
replaced =true;
}
return replaced;
}
/**
* Insert a value into the array. From the insertion point on, the
* data needs to be copied down one space to make room for the new
* element indices .
*
* @param index: The index where the new element should be inserted
* @param element: The element to insert in the array
* @param array: The array to insert into
* @param indicesUsed: the number of elements in the array already used
* @return the array with the new value inserted
*/
publicString[] insert(int index,String element,String[] array,int indicesUsed){
// First, check that there is enough room in the array for
// another element. The parameter indicesUsed indicates how
// many elements of the array are already filled.
String[] newArray = array;
if( indicesUsed == array.length ){
newArray = makeBiggerArray(array);
}
// Now that we know there is enough room, move the
// elements down one spot in the array until we get to the
// insertion point. Make sure the element at the insertion
// point gets moved too.
int indexToMove = indicesUsed;
while( indexToMove >= index ){
newArray[indexToMove+1]= newArray[indexToMove];
indexToMove--;
}
// Set the given index to the new element
newArray[index]= element;
// Return the new array
return newArray;
}
/**
* Since arrays are fixed size, if you run out of room, the
* only options is to create a new bigger array, then copy
* the old data over.
* @param oldArray: The old data to copy into the new array
* @return The new bigger array
*/
publicString[] makeBiggerArray(String[] oldArray){
// Make the new array twice the size of the old one.
int newArraySize .
1. week4_src/ArrayMethods.javaweek4_src/ArrayMethods.javapac
kage edu.drexel.ct290;
publicclassArrayMethods{
/**
* Pass and array to a method that will search through it for a
given value.
*
* @param elementToFind: This is the item we want to find i
n the array.
* @param stringArray: This is the array to search through.
* @return Returns: The index of the element, or -
1 if not found.
*/
publicint findElement(String elementToFind,String[] stringArra
y ){
int index =-1;
for(int i=0; i<stringArray.length; i++){
if( elementToFind.equals(stringArray[i])){
// If the element is found, set index
index = i;
}
}
return index;
}
/**
* Replace a value in an array.
*
* @param indexToReplace: The index where the new value s
hould be written.
2. * @param newValue: The new value.
* @param stringArray: Change a value in this array.
* @return True if the given index is valid
*/
publicboolean replace(int indexToReplace,String newValue,Stri
ng[] stringArray ){
boolean replaced =false;
// Use the if condition to make sure the given index is valid
if( indexToReplace >0&& indexToReplace < stringArray.length
){
stringArray[indexToReplace]= newValue;
replaced =true;
}
return replaced;
}
/**
* Insert a value into the array. From the insertion point on, t
he
* data needs to be copied down one space to make room for t
he new
* element indices .
*
* @param index: The index where the new element should b
e inserted
* @param element: The element to insert in the array
* @param array: The array to insert into
* @param indicesUsed: the number of elements in the array
already used
* @return the array with the new value inserted
*/
publicString[] insert(int index,String element,String[] array,int i
ndicesUsed){
// First, check that there is enough room in the array for
3. // another element. The parameter indicesUsed indicates how
// many elements of the array are already filled.
String[] newArray = array;
if( indicesUsed == array.length ){
newArray = makeBiggerArray(array);
}
// Now that we know there is enough room, move the
// elements down one spot in the array until we get to the
// insertion point. Make sure the element at the insertion
// point gets moved too.
int indexToMove = indicesUsed;
while( indexToMove >= index ){
newArray[indexToMove+1]= newArray[indexToMove];
indexToMove--;
}
// Set the given index to the new element
newArray[index]= element;
// Return the new array
return newArray;
}
/**
* Since arrays are fixed size, if you run out of room, the
* only options is to create a new bigger array, then copy
* the old data over.
* @param oldArray: The old data to copy into the new array
* @return The new bigger array
*/
publicString[] makeBiggerArray(String[] oldArray){
// Make the new array twice the size of the old one.
int newArraySize = oldArray.length *2;
String[] newArray =newString[newArraySize];
4. // Copy the old data over to the new array
for(int i=0; i<oldArray.length; i++){
newArray[i]= oldArray[i];
}
return newArray;
}
publicvoid printArray(String[] array ){
for(int i=0; i<array.length; i++){
System.out.println("Element "+ i +": "+ array[i]);
}
}
publicstaticvoid main(String[] args){
String[] groceries =newString[5];
groceries[0]="bread";
groceries[1]="milk";
groceries[2]="cheese";
groceries[3]="steak";
groceries[4]="portobello mushrooms";
ArrayMethods arrayMethods =newArrayMethods();
arrayMethods.printArray(groceries);
int cheese = arrayMethods.findElement("cheese", groceries);
// You could use an if condition to make sure the result is not -1
System.out.println("nCheese is at index: "+ cheese +"n");
arrayMethods.replace(4,"lobster", groceries);
arrayMethods.printArray(groceries);
System.out.println("nInsert onion into the list.n");
5. String[] withOnion = arrayMethods.insert(3,"onion",groceries,5
);
arrayMethods.printArray( withOnion );
// make a blank line so the output is easier to read
System.out.println("");
String[] moreGroceries = arrayMethods.makeBiggerArray(groce
ries);
arrayMethods.printArray(moreGroceries);
}
}
__MACOSX/week4_src/._ArrayMethods.java
week4_src/Contacts.javaweek4_src/Contacts.javapackage edu.dr
exel.ct290;
import java.util.ArrayList;
publicclassContacts{
// Collects can hold any type of object, but its best to tell
// Java what type of object it will be. To do that put the
// class name in angle brackets right after the collection type.
// In this case we will have an ArrayList that holds objects of ty
pe
// Person (same as the Person class used in week 3).
privateArrayList<Person> contacts =newArrayList<Person>();
publicvoid addContact(Person newPerson ){
// To add an element to an ArrayList simply call the add method
.
6. // With array list, you don't have to worry about the size.
contacts.add(newPerson);
}
publicint find(Person person ){
// Find the index of a given object.
// indexOf returns -1 if not found
return contacts.indexOf(person);
}
publicvoid delete(Person person ){
// simply use remove to delete an element from the list
// There is also a remove method that removes based on index
contacts.remove(person);
}
publicvoid deleteAll(){
// ArrayList's clear method removes all elements from the list.
contacts.clear();
}
publicvoid printAll(){
// Use size() to get the number of elements in the list.
for(int i=0; i<contacts.size(); i++){
Person contact = contacts.get(i);
System.out.println("n"+ contact.toString());
}
}
}
week4_src/FirstArray.javaweek4_src/FirstArray.javapackage ed
u.drexel.ct290;
publicclassFirstArray{
7. publicstaticvoid main(String[] args){
// Create an array of five strings
String[] groceries =newString[5];
// After creation, the array is empty, so lets fill it up:
// Recall that array index go from 0 to size-1. In this case 0-4
// Using brackets with an index references a specific element.
groceries[0]="bread";
groceries[1]="milk";
groceries[2]="cheese";
groceries[3]="steak";
groceries[4]="portobello mushrooms";
// Now print out each value in the array. For loops are ideal
// for arrays since we know the exact number of iterations.
// Here we start with element, 0, and keep incrementing i
// as long as i is less than 5
for(int i=0; i<5; i++){
System.out.println("Element "+ i +": "+ groceries[i]);
}
// Array also have properties you can access with the . operator
// Most notably the lenght of the array:
System.out.println("Array size: "+ groceries.length);
// Trying using groceries.length in the for loop above instead of
// using the hard coded size.
// Most array errors occur due to using an invalid index:
// Uncomment the line below, then see what happens when you r
un it:
//System.out.println("More groceries: " + groceries[6]);
}
}
8. __MACOSX/week4_src/._FirstArray.java
week4_src/GradeBook.javaweek4_src/GradeBook.javapackage e
du.drexel.ct290;
import java.util.ArrayList;
import java.util.Scanner;
publicclassGradeBook{
privateString course;
privateArrayList<Person> students =newArrayList<Person>();
privateArrayList<GradeBookEntry> entries =newArrayList<Gra
deBookEntry>();
publicString getCourse(){
return course;
}
publicvoid setCourse(String course){
this.course = course;
}
publicvoid addStudent(Person student ){
students.add(student);
}
publicvoid addEntry(){
// Print out each students name and choose one
System.out.println("Grade which student: ");
for(int i=0; i<students.size(); i++){
System.out.println(i +" "+ students.get(i).getName());
}
9. Scanner reader =newScanner(System.in);
reader.nextInt();
reader.nextLine();
// TODO: get the assessment name and numeric grade
GradeBookEntry entry =newGradeBookEntry();
// TODO: set the data in the new entry
entries.add(entry);
}
publicvoid listGrades(){
// TODO: Print out all the grade entries in this gradbook
}
publicvoid displaySummary(){
// TODO: show a distribution of letter grades in this class.
// See the barchart example in Java HTP 7.4
}
publicstaticvoid main(String[] args){
Person person1 =newPerson();
person1.setName("John");
Person person2 =newPerson();
person1.setName("Lisa");
Person person3 =newPerson();
person1.setName("Bill");
Person person4 =newPerson();
person1.setName("Sarah");
GradeBook book =newGradeBook();
10. book.setCourse("CT-290");
// TODO: add some gradbook entries
// TODO: list the entries and disply the bar chart
}
}
week4_src/GradeBookEntry.javaweek4_src/GradeBookEntry.jav
apackage edu.drexel.ct290;
import java.util.Scanner;
publicclassGradeBookEntry{
privatePerson student;
privateint numericGrade;
privateString assessmentName;
// The next six methods are just getters and setters
// for the member variables of this class.
publicPerson getStudent(){
return student;
}
publicvoid setStudent(Person student){
this.student = student;
}
publicint getNumericGrade(){
return numericGrade;
}
publicvoid setNumericGrade(int numericGrade){
11. this.numericGrade = numericGrade;
}
publicString getAssessmentName(){
return assessmentName;
}
publicvoid setAssessmentName(String assessmentName){
this.assessmentName = assessmentName;
}
publicString getLetterGrade(){
GradeConverter converter =newGradeConverter();
return converter.convertGrade(numericGrade);
}
publicvoid printEntry(){
System.out.println(student.toString());
// instantiate a GradeConverter to get the letter grade.
GradeConverter converter =newGradeConverter();
System.out.println("Scored "+ numericGrade );
System.out.println("Which is a: "+ converter.convertGrade(num
ericGrade));
System.out.println("For assessment: "+ assessmentName);
}
publicstaticvoid main(String[] args){
// instantiate the GradeBookEntry, just like we have
// done with the Scanner class.
GradeBookEntry gradeBookEntry =newGradeBookEntry();
Scanner reader =newScanner(System.in);
// instantiate a new person object
Person student =newPerson();
student.getPersonData();
13. return"D";
}
else{
return"F";
}
}
/**
* This method gets input from the user
* @return a grade in number format from 0-100
*/
publicint getNumberGrade(){
// declare and intialize variables
int userInput=0;
Scanner reader =newScanner(System.in);
// get the user input
System.out.print("Enter the number grade: ");
userInput = reader.nextInt();
// return the input to the caller of this method
return userInput;
}
/**
* @param args
*/
publicstaticvoid main(String[] args){
GradeConverter converter =newGradeConverter();
int input = converter.getNumberGrade();
String letterGrade = converter.convertGrade(input);
System.out.println("The letter grade for "+ input +" is "+ letter
Grade);
}
}