Monday, January 16, 2012

Java Programming Assignment #1

Questions

Computer Science 151 Assignment 1

Each of the following problems is worth 5 points (total 15 Points). Use JCreator (or other IDE if you’re not a Windows user). to type, compile and run the following programs.

 1. Type the sample program included at the end this assignment into a file called MyQueue.java (using the JCreator editor). Then compile the code and, if there are no errors, run it to see how it operates.
You are not expected to understand the details of this program. Basically, it creates a simple data structure called a “queue” (British word for a line-up) in which numbers entered from the keyboard are stored in a “first-in first-out” order. The program then displays the first and last elements from the queue, followed by the rest of the elements in sequence.
(5 points)
2. After you ge tMyQueue.java working, make a duplicate copy of the file naming it MyQueue2.java. Modify MyQueue2 so it displays:
- the first two elements from the queue - the last two elements from the queue - the rest of the elements in the queue
Compile and run the modified program to make sure it works.    (5 points)
3. Write a simple program that prompts the user for an amount of money in Canadian dollars and then converts and displays the equivalent amount in Euros. (The GasMileage or TempConverter programs from Chapter 2 might be helpful as examples). Your program should also print the following information at the top:
a. Your name b. Your student ID# c.    Your class section (“C SC 151A” or “C SC 151B”)
How to submit your assignment:
Print all source code (.java files) on letter-sized (8.5x11 inch). Label each question clearly, sort the pages and add a cover page containing:
• • • •
Your name Your student ID Course and section (MW=”A”, TTh=”B”) The assignment number
Staple the pages together and place the assignment in a 9x12 envelope with your Name, Course number, section (“A” or “B”) and assignment number written neatly on the outside.    Please don’t seal the envelope. Submit the assignment in class or place it in the box outside Dr. Howard’s office (Cass 408).
Include in your envelope a CD/DVD containing the source code (.java) files and bytecode (.class) files.
Note: You will need 8 CD/DVDs in this course for your assignments.

Solutions

Question #1

//----------------------------------------------
//MyQueue.java - Sample code for As1, Question 1
//----------------------------------------------

import java.io.*;
import java.util.*;

public class MyQueue
{
    LinkedList<Integer> list;
    int num;

    public MyQueue()
    {
        list = new LinkedList<Integer>();
    }

    public void make()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("How many elements? ");
        num = keyboard.nextInt();
        if(num == 0)
        {
            System.out.println("You entered zero ... nothing to do");
            System.exit(0);
        }
        else
        {
            System.out.println("Enter elements : ");
            for(int i = 0; i < num; i++)
            {
                int n = keyboard.nextInt();
                list.add(n);
            }
        }
        System.out.println("First element:" + list.removeFirst());
        System.out.println("Last element:" + list.removeLast());
        System.out.println("Rest of elements:");
        while(!list.isEmpty())
        {
            System.out.println(list.remove() + "\t");
        }
    }

    public static void main(String[] args)
    {
        MyQueue q = new MyQueue();
        q.make();
    }
}

Question #2

//----------------------------------------------
//MyQueue.java - Sample code for As1, Question 1
//----------------------------------------------

import java.io.*;
import java.util.*;

public class MyQueue2
{
    LinkedList<Integer> list;
    int num;

    public MyQueue2()
    {
        list = new LinkedList<Integer>();
    }

    public void make()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("How many elements? ");
        num = keyboard.nextInt();
        if(num == 0)
        {
            System.out.println("You entered zero ... nothing to do");
            System.exit(0);
        }
        else
        {
            System.out.println("Enter elements : ");
            for(int i = 0; i < num; i++)
            {
                int n = keyboard.nextInt();
                list.add(n);
            }
        }
        System.out.println("First element:" + list.removeFirst());
        System.out.println("Second element:" + list.removeFirst());
        System.out.println("Last element:" + list.removeLast());
        System.out.println("Second Last element:" + list.removeLast());
        System.out.println("Other elements:");
        while(!list.isEmpty())
        {
            System.out.println(list.remove() + "\t");
        }
    }

    public static void main(String[] args)
    {
        MyQueue2 q = new MyQueue2();
        q.make();
    }
}

Question #3

//*************************************
//CanadadiantoEuroConversion.Java
// C SC 151A
//*************************************
import java.io.*;
import java.util.*;
public class CanadiantoEuroConversion
{
    //---------------------------------------------------------------
    // Converts user inputted amount of Canadian dollars to Euro's
    //---------------------------------------------------------------
    public static void main (String[] args)
    {

        System.out.println("How much Canadian?");
        {
            int num;
            Scanner keyboard = new Scanner(System.in);
            num = keyboard.nextInt();
            final double CONVERSION_FACTOR = 1.3594;
            int Canadianamount = num;
            double Euroamount;
            Euroamount = Canadianamount * CONVERSION_FACTOR;
            System.out.println ("Canadian Dollars: " + Canadianamount);
            System.out.println ("Euro's: " + Euroamount);

        }
    }
}

No comments:

Post a Comment