Monday, May 9, 2011

Final Exam Code

Hello World!

I might as well give everyone an update as to what's going on for the summer with me.

I'll be working Full-Time (and then some) as a landscaper for my old boss in Montreal to save up some money for my future endeavors next semester, but aside from that I have almost finished my 2nd semester in Computer Sciences at my school, just another 2 years to go, and then I'm off into my field in the workforce (enough of this part-time doughnut shop stuff).

I also thought it'd be a good idea to post the code from my final exam. It's a simple program, but it's from the programming portion of the final exam I had (compared to the multiple choice and True/False portions). Code is originally mine, though I'm sure a lot of my classmates got similar answers.

Here is the correct code for the program to work according to the outline of the exam:

package Programming_2;
import java.util.Scanner;
import java.io.*;

/**
 * @author A. Wright
 * @version 1.0
 * @date May 9, 2011
 * This program was on the final exam for the author's programming course.
 * The following is the correct code made to have a number of products inputed with
 * serial numbers, pricing information, and quantities for each product. The returned
 * value is the total quantity of items among the different products.
 */

public class Final_Exam_Question {

    public static void main(String[] args)
    {
        Scanner kb = new Scanner(System.in);
        String[] productID;
        double[] price;
        int[] quantity;
        int size = 0; //Default
       
        System.out.println("Please enter the number of products: ");
        size = kb.nextInt();
       
        productID = new String[size];
        price = new double[size];
        quantity = new int[size];
       
        for(int i = 0; i < size; i++)
        {
            System.out.println("Please enter the following information for product: "+(i+1));
            System.out.println("Product ID Number: ");
            productID[i] = kb.next();
            System.out.println("Price: ");
            price[i] = kb.nextDouble();
            System.out.println("Quantity: ");
            quantity[i] = kb.nextInt();
        }       
        System.out.println("The total quantity of products is: "+totalQuantity(quantity));       
    }
   
    public static int totalQuantity(int[] qty)
    {
        int total = 0;       
        for(int j = 0; j < qty.length; j++)
        {
            total += qty[(j)];           
        }
        return total;
    }   
}

Till next time.

Hold Your Head High
~DanceLink

No comments:

Post a Comment