Monthly Archives: June 2020

OOD Project: gpa calculator

Classwork:
Write a program, YI_GPACalculator.java to read grades from a text file. The grades will have a specific structure so the marking periods, the subjects, and categories can be identified.

The file structure given here is just an example. You should notice it has some redundancy. You can configure your own structure to enable easier implementation.

[spoiler title=’gpadata.txt’]
Q1
Subject: English
Categories start:
Homework: .30
Classwork: .30
Test: .40
Categories end
Data:
Homework:
75
90
85
Classwork:
65
75
78
Test:
100
98
99
Subject: Math
Categories start:
Homework: .25
Classwork: .25
Test: .50
Categories end
Data:
Homework:
86
88
100
Classwork:
60
70
80
Test:
100
90
91
Q1 end
Q2
Subject: English
Categories start:
Homework: .30
Classwork: .30
Test: .40
Categories end
Data:
Homework:
95
80
81
Classwork:
55
95
88
Test:
93
100
85
Subject: Math
Categories start:
Homework: .25
Classwork: .25
Test: .50
Categories end:
Homework:
86
88
100
Classwork:
60
70
80
Test:
87
96
81
Q2 end
[/spoiler]

Here is some code that might help you read the file.

  1. Using PU libraries: StdIn.java
/**
 * Calculate the GPA given a file with all the requirements.
 * Q1
 * Subject: English
 * Categories start:
 * Homework: .30
 * Classwork:
 * ....
 * ....
 * END
 * 
 * java GPACalculator1 < gpadata.txt
 * 
 */
public class GPACalculator1
{
    public static void main(String [] args)
    {

            while (StdIn.hasNextLine()) {
                String s1 = StdIn.readLine();
                System.out.println(s1);
            }
          
    
    }
    
}

  1. Using java libraries:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
 * Calculate the GPA given a file with all the requirements.
 * Subject: English
 * Catgories beginning:
 * 
 */
public class GPACalculator2
{
    public static void main(String [] args)
    {
        
        File file = new File(args[0]);
    
        try {
    
            Scanner sc = new Scanner(file);
    
            while (sc.hasNextLine()) {
                String s1 = sc.nextLine();
                System.out.println(s1);
            }
            sc.close();
        } 
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    
    }
    
}

Before you get started writing code,
1. Draft a flowchart or a concise pseudocode.
2. Trace few grades from one subject for Q1 and Q2.
3. Manually calculate the GPA for the data used in the trace.
4. Confirm that your trace and calculation coincide.

Submit all your work to edmodo.com.

OOD – Colliding Balls Simulation

Follow these steps:

  1. Download the attached BlueJ project file, Collision.zip to your java folder. Double click on it.
  2. Open the folder and double-click the BlueJ package icon.
  3. Replace “_YI” suffix with your initials in all the files that use it.
  4. Study the code for the Ball_YI, BallDriver_YI, and CollidingBall_YI classes.
  5. Modify the Ball_YI class as follows:
    — a. When any ball collides with another, the ball changes direction. Their behavior after the collision is up to you but it has to be noticeable. There are two types of collisions, elastic and inelastic. It is also up to you to choose the type.
    — b. When any ball collides with each other, the ball changes to a random color and stays that color until it collides again.
    — c. When any ball collides with the walls, the ball changes to a random color. NOTE: It is ok if all the balls change color at the same time.

    Submission:
  6. A short QuickTime video.
  7. Attach Ball_YI, and CollidingBall_YI classes.

Collision Detection:

 These diagram should help you with the calculations for the conditions you need to implement to capture the the balls within a “d” distance of each other:

   or

More Hints: look at the problem from the cartessian plane perspective:

OOD: ArrayList – Intro

/**
 * Test of ArrayLists basic instructions
 * 
 * @ge
 * @4/16/18
 */
import java.util.ArrayList;

public class ArrayListTest
{
   public static void main(String [] args)
   {
        
       ArrayList myList = new ArrayList();
       //ArrayList myList = new ArrayList(); // more efficient - eliminates warning
       Integer x = new Integer(45); // creates Integer 45
       
       myList.add(x); // adds Integer 45 to myList
       myList.remove(x); // removes the value 45 from myList
       x = new Integer(100); // creates Integer 100 
       myList.add(0,x); // adds Integer 100 to the beginning of myList

       System.out.println(myList.get(0)); // print the first element 
       System.out.println(myList.size()); // print the number of elements
       
       // One step further
       myList.add(5);
       myList.add(-27);
       myList.add(55);
       myList.add(0,3);
   }
}

Classwork:

Let’s use the following documentation Java™ Platform, Standard Edition 8
API Specification to create our own resources: 

ArrayList

From the documentation copy and paste the following methods’ description:

* 1
* boolean add(E e)
*
* 2
* void add(int index, E element)

* Having two methods with the same name but different argument list
* is called OVERLOADING
*
* 3
* E get(int index)
*
* 4
* E set(int index, E element)
*
* 5
* int indexOf(Object o)
*
* 6
* boolean isEmpty()
* Returns true if this list contains no elements.
*
* 7
* E remove(int index)
*
* 8
* boolean remove(Object o)
*
* 9
* int size()

Resource:
Screen Shot 2016-01-15 at 7.46.11 AM

Assignment:
ArrayListBasics_YI.java
Write a program that does the following:
Choose a meaningful and complete message for each item in the list.

  1. It creates an ArrayList, anAList of 100 random integers between any values from 1 to 1000 and find the two consecutive integers with the smallest difference between the two of them.

  2. It finds and prints with a message the largest value in anAList.

  3. It finds and prints with a message the average value in anAList.

  4. It prints all the numbers from anAList separated with a space and in one line.

  5. It prints the numbers from anAlist separated by 3 spaces in 10 columns by 10 rows format. Use printf to achieve this format. Do not use tab: “\t”.

  6. Prompt the user for an integer and replace the 10th Integer with this new one. Display a message to acknowledge wether it was found.

  7. Prompt the user for a number and search for that number. Display the number and the index where it was found.

  8. Remove 10th Integer and print a message with the number.

  9. Print the number of elements the ArrayList anAlist contains.

Note: Use printf to keep all your numbers tab properly.

/**** Make sure your documentation includes the output ****/