Weeblyjcprportfolio.weebly.com/uploads/2/8/5/8/28581727/assig…  · Web viewIn this word file,...

10
Assignment 1 (Due 09/19/2012) COSC 237-003 Name: Jean Polanco-Ramos ID: 045770 All the program code must be placed a word document (preferred) and submitted via Blackboard. Please put output of each program in the word document. In this word file, please name your document with your own name (FirstName_LastName.doc). Include your name, your email address, Course number and the Assignment number on first page of your document. If you have any question, please email to TA at [email protected] and cc to me Problem 1.1 Write a program that prompts the user to input the length and breadth of a rectangle and then prints the rectangle’s area? import java.util.*; public class Problem_1 { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter length: "); double length = input.nextDouble(); System.out.print("Enter breadth: ");

Transcript of Weeblyjcprportfolio.weebly.com/uploads/2/8/5/8/28581727/assig…  · Web viewIn this word file,...

Page 1: Weeblyjcprportfolio.weebly.com/uploads/2/8/5/8/28581727/assig…  · Web viewIn this word file, please name your document with your own name (FirstName_LastName.doc). Include your

Assignment 1 (Due 09/19/2012)

COSC 237-003

Name: Jean Polanco-Ramos

ID: 045770

All the program code must be placed a word document (preferred) and submitted via Blackboard. Please put output of each program in the word document.

In this word file, please name your document with your own name (FirstName_LastName.doc). Include your name, your email address, Course number and the Assignment number on first page of your document.

If you have any question, please email to TA at [email protected] and cc to me

Problem 1.1

Write a program that prompts the user to input the length and breadth of a rectangle and then prints the rectangle’s area?

import java.util.*;

public class Problem_1 {

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print("Enter length: ");

double length = input.nextDouble();

System.out.print("Enter breadth: ");

double breadth = input.nextDouble();

double perimeter = breadth+length;

System.out.println("Perimeter: " + perimeter);

Page 2: Weeblyjcprportfolio.weebly.com/uploads/2/8/5/8/28581727/assig…  · Web viewIn this word file, please name your document with your own name (FirstName_LastName.doc). Include your

}

}

Problem 1.2 (Programming Exercises (Chapter 3, Exercise 3 pp-165))

The manager of a football stadium wants you to write a program that calculates the total ticket sales after each game. There are four types of tickets-box, sideline, premium, and general admission. After each game, the data is stored in a file in the following form:

ticketPrice numberOfTicketSold

…….

Sample data are shown below:

250 5750

100 28000

50 35750

25 18750

Page 3: Weeblyjcprportfolio.weebly.com/uploads/2/8/5/8/28581727/assig…  · Web viewIn this word file, please name your document with your own name (FirstName_LastName.doc). Include your

The first line indicates that the box ticket price is $250 and that 5750 tickets were sold at that price. Please type above information into a text file and write program to read those data and output the number of tickets sold and the total sale amount on the screen.

import java.util.*; import java.io.*; public class Problem_2 {     public static void main(String[] args) throws FileNotFoundException     {   Scanner inFile = new Scanner (new FileReader ("h:\\ticketSales.txt"));                        double iPrice;            double iSold;            double iSum;            double runningSum = 0;            double runningPrice = 0;                       iPrice= inFile.nextDouble();            iSold = inFile.nextDouble();            iSum = iPrice * iSold;            runningSum = runningSum + iSold;            runningPrice = runningPrice + iPrice;            System.out.println ("Total tickets sold:" + iSold);           System.out.println("Total Sales: "+ iSum);           System.out.println();            iPrice= inFile.nextDouble();            iSold = inFile.nextDouble();            iSum = iPrice * iSold;            runningSum= runningSum+ iSold;            runningPrice= runningPrice+ iSum;            System.out.println ("Total tickets sold:" + iSold);           System.out.println("Total Sales: "+ iSum);           System.out.println();            iPrice= inFile.nextDouble();            iSold = inFile.nextDouble();            iSum = iPrice * iSold;            runningSum= runningSum+ iSold;            runningPrice= runningPrice+ iSum;           

Page 4: Weeblyjcprportfolio.weebly.com/uploads/2/8/5/8/28581727/assig…  · Web viewIn this word file, please name your document with your own name (FirstName_LastName.doc). Include your

            System.out.println ("Total tickets sold:" + iSold);            System.out.println("Total Sales: "+ iSum);            System.out.println();             iPrice= inFile.nextDouble();            iSold = inFile.nextDouble();            iSum = iPrice * iSold;            runningSum= runningSum+ iSold;            runningPrice= runningPrice+ iSum;             System.out.println ("Total tickets sold:" + iSold);            System.out.println("Total Sales: "+ iSum);            System.out.println();            System.out.println ("Total tickets sold: " + runningSum);            System.out.println ("Total Sales: " + runningPrice);          }     }

Problem 1.3

Page 5: Weeblyjcprportfolio.weebly.com/uploads/2/8/5/8/28581727/assig…  · Web viewIn this word file, please name your document with your own name (FirstName_LastName.doc). Include your

Write a program that converts date formats from American Format: “September 10, 2012” to International Format; - such as "2012-September-10”.

Hint: you may use several string methods, including:

(1) str.indexOf(c): given a string str and a character c, this finds the position (starting at 0) of the first instance of c in str.

(2) str.substring(p): given a string str and a position p, this method returns a new string starting at position p and going until the end of the string.

You can solve this problem in the follow way. You should first look for everything that comes before the first space. This will be the month. Save this in a String variable, and then use substring to find everything that comes after this point -the remainder of the string. Working from this remainder, you can search for the comma (using indexOf) to find the piece that has the date. Once you have this, everything after the comma is the year. You can then using string concatenation (with the + operator) to tie the year, month and date together like "2008-January-30”.

public class Problem_3 {

public static void main(String[] args) {

String date = "September 10, 2012";

int iChange = date.indexOf(" ");

String sWord = date.substring(0,iChange);

int iComma = date.indexOf(',') ;

String sDay = date.substring(iChange+1,iComma);

String sYear = date.substring(date.lastIndexOf(',') + 1);

System.out.println(sYear+"-"+sWord+"-"+sDay);

Page 6: Weeblyjcprportfolio.weebly.com/uploads/2/8/5/8/28581727/assig…  · Web viewIn this word file, please name your document with your own name (FirstName_LastName.doc). Include your

}

}

Problem 1.4

Write a program that prompts the user to input an integer between 0 and 35. If the number is less than or equal to 9, the program should output the number; otherwise, it should output A for 10, B for 11, C for 12,…, Z for 35. (Hint: use the case operator (char)(number+55))

import com.sun.xml.internal.bind.v2.TODO;

import java.util.Scanner;

/**

*

* @author jpolan3

Page 7: Weeblyjcprportfolio.weebly.com/uploads/2/8/5/8/28581727/assig…  · Web viewIn this word file, please name your document with your own name (FirstName_LastName.doc). Include your

*/

public class JavaApplication1 {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int iNum;

System.out.println("Enter number (1-35): ");

iNum = input.nextInt();

if (iNum <= 9){

System.out.println("Digit: "+ iNum);

}

else {

System.out.println("Character: "+(char)(iNum + 55));

}

// TODO code application logic here

}

Page 8: Weeblyjcprportfolio.weebly.com/uploads/2/8/5/8/28581727/assig…  · Web viewIn this word file, please name your document with your own name (FirstName_LastName.doc). Include your

}

Page 9: Weeblyjcprportfolio.weebly.com/uploads/2/8/5/8/28581727/assig…  · Web viewIn this word file, please name your document with your own name (FirstName_LastName.doc). Include your