Demo2.java
public class Demo2 {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++)
System.out.println("You entered " + args[i]);
}
}
public class Demo2 {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++)
System.out.println("You entered " + args[i]);
}
}
import java.util.*;
public class WrapperDemo
{
public static void main(String[] args)
{
ArrayList numList = new ArrayList();
numList.add(2006);
int num;
// num = numList.get(0); this won't work
num = (Integer)numList.get(0);
System.out.println(num);
}
}
import java.util.*;
public class BookTest {
public static void main(String[] args) {
ArrayList aList = new ArrayList();
Book b1 = new Book("Catcher In The Rye");
Book b2 = new Book("Crime and Punishment");
aList.add(b1);
aList.add(b2);
for (int i = 0; i < aList.size(); i++)
{
// System.out.println(aList.get(i).getName()); THIS WON'T WORK
Book b = (Book) aList.get(i); // ArrayList object must be typecast as Book
System.out.println(b.getName());
}
System.out.println("There are " + Book.getCount() + " books.");
}
}
public class Total
{
public static void main(String[] args)
{
int total = 0;
for(int i = 0; i < args.length; i++)
total += Integer.parseInt(args[i]);
System.out.println("total equals " + total);
}
}
public class Book
{
private String name;
static int count = 0;
public Book()
{
count++;
}
public Book(String name)
{
this.name = name;
count++;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public String toString()
{
return name;
}
public static int getCount()
{
return count;
}
}
public class Demo {
public static void main(String[] args) {
System.out.println("You entered " + args[0]);
}
}
public class ArraysAsParameters
{
public static void main(String[ ] args)
{
String president = "Bill Clinton";
String[] list = {"John","Paul","George","Pete"};
change(president);
change(list);
System.out.println(president);
for(int k = 0; k < list.length; k++)
System.out.println(list[k]);
}
public static void change(String[] list)
{
list[3] = "Ringo";
}
public static void change(String president)
{
president = "George Bush";
}
}
public class Multiply {
public static void main(String[] args) {
int num1 = Integer.parseInt(args[0]);
int num2 = Integer.parseInt(args[1]);
int product = num1 * num2;
System.out.println("Product equals " + product);
}
}
import java.util.*;
public class CreateArrayLists
{
public static void main(String[ ] args)
{
ArrayList planets = new ArrayList();
planets.add("Mercury");
planets.add("Venus");
planets.add("Mars");
planets.add("Earth");
for(int k = 0; k < planets.size(); k++)
System.out.println(planets.get(k));
System.out.println();
planets.set(2,"Earth");
planets.set(3,"Mars");
Iterator it = planets.iterator(); // ArrayLists implement the List interface
while (it.hasNext()) {
System.out.println(it.next());
}
String s = (String)planets.get(2);
System.out.println(s);
}
}
public class CreateArrays
{
public static void main(String[] args)
{
int[] intArray = new int[5]; // create array using new operator
int[] intArray2 = {1492, 1776, 1860, 1917, 1945}; // initalizer list
String[] stringArray = new String[5];
String[] stringArray2 = {"mon","tue","wed","thu","fri"};
printIntArray(intArray);
printIntArray(intArray2);
printStringArray(stringArray);
printStringArray(stringArray2);
}
public static void printIntArray(int[] list)
{
for(int i = 0; i < list.length; i++)
System.out.println(list[i]);
System.out.println();
}
public static void printStringArray(String[] list)
{
for(int i = 0; i < list.length; i++)
System.out.println(list[i]);
System.out.println();
}
}
import cs1.*;
import java.text.*;
public class Server {
private String name;
public Server(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString() {
return name;
}
public void greet() {
System.out.println("Hello, my name is " + name);
System.out.println("I will be your server today.");
}
public void serve(Customer c, Menu m) {
System.out.println();
System.out.println("Menu");
m.display();
for (;;) {
System.out.print("Please select from the menu (0 to quit): ");
int item = Keyboard.readInt();
if (item <= 0) {
break;
}
if (item > m.numberOfMenuItems()) {
System.out.println("Sorry, that's not a valid item number.");
} else {
c.order(item);
}
}
System.out.println("\n" + "Order summary for customer #" +
c.getNumber() + ": ");
c.displayOrder();
double subtotal = c.getSubtotal();
NumberFormat money = NumberFormat.getCurrencyInstance();
System.out.println("Subtotal = \t" + money.format(subtotal));
System.out.println("Tax = \t\t" + money.format(c.getTax(subtotal)));
System.out.println("Total = \t" + money.format(c.getTotal(subtotal)));
System.out.println("===================================");
}
}
import java.text.*;
import java.util.*;
public class Menu {
ArrayList item = new ArrayList();
ArrayList price = new ArrayList();
public Menu() {
}
public void display() {
NumberFormat money = NumberFormat.getCurrencyInstance();
for (int i = 0; i < item.size(); i++) {
System.out.println((i + 1) + "\t" + item.get(i) + "\t" +
money.format(price.get(i)));
}
System.out.println();
}
public void addItem(String s, double d) {
item.add(s);
price.add(new Double(d));
}
public String getItem(int i) {
return (String) item.get(i);
}
public double getPrice(int i) {
return ((Double) price.get(i)).doubleValue();
}
public int numberOfMenuItems() {
return item.size();
}
}
import java.util.*;
import cs1.*;
public class Restaurant {
public static void main(String[] args) {
Locale.setDefault(Locale.US); // So monetary output uses $
Orders orders = new Orders();
Menu m1 = new Menu();
boolean nextCustomer = false;
String response;
m1.addItem("Hamburger", 2.50);
m1.addItem("French fries", 1.50);
m1.addItem("Soft drink", 1.00);
Server s1 = new Server("Kevin");
do{
Customer c = new Customer(m1);
orders.addCustomer(c);
s1.greet();
s1.serve(c, m1);
System.out.print("Next customer? (y/n): ");
response = Keyboard.readString();
nextCustomer = response.equals("y");
} while (nextCustomer);
orders.displayOrders();
orders.displayTotal();
}
}
import java.text.*;
import java.util.*;
public class Customer {
final static double TAXRATE = 0.0825;
private static int nextCustomerNumber;
private int customerNumber;
private ArrayList orderList = new ArrayList();
private Menu menu; // the menu the customer is ordering from
private double total;
public Customer(Menu m) // customer will order from menu m
{
menu = m;
customerNumber = nextCustomerNumber + 1;
nextCustomerNumber++;
}
public int getNumber() {
return customerNumber;
}
public String toString() {
return Integer.toString(customerNumber);
}
public void order(int item) {
// If the menu has N items, someone orders an item using a number
// from 1 through N. Internally, our program will represent these
// using the numbers 0 through N-1.
orderList.add(new Integer(item - 1));
}
public void displayOrder() {
NumberFormat money = NumberFormat.getCurrencyInstance();
for (int i = 0; i < orderList.size(); i++) {
int itemNumber = ((Integer) orderList.get(i)).intValue();
System.out.println(menu.getItem(itemNumber) + "\t" +
money.format(menu.getPrice(itemNumber)));
}
System.out.println();
}
public double getSubtotal() {
double subtotal = 0.0;
for (int i = 0; i < orderList.size(); i++) {
int itemNumber = ((Integer) orderList.get(i)).intValue();
subtotal += menu.getPrice(itemNumber);
}
return subtotal;
}
public double getTax(double subtotal) {
return subtotal * TAXRATE;
}
public double getTotal(double subtotal) {
total = subtotal + getTax(subtotal);
return subtotal + getTax(subtotal);
}
public double returnTotal() {
return total;
}
}
import java.text.*;
import java.util.*;
public class Orders {
private ArrayList customerList = new ArrayList();
private Customer c;
private double totalSales = 0.0;
public Orders() {
}
public void addCustomer(Customer c){
customerList.add(c);
}
public void displayOrders(){
System.out.println("All Orders");
System.out.println("----------");
for(int i = 0; i < customerList.size(); i++){
c = (Customer)customerList.get(i);
c.displayOrder();
}
}
private double calcTotalSales(){
for(int i = 0; i < customerList.size(); i++){
c = (Customer)customerList.get(i);
totalSales += c.returnTotal();
}
return totalSales;
}
public void displayTotal()
{
NumberFormat money = NumberFormat.getCurrencyInstance();
System.out.println("Total Sales (including tax)");
System.out.println("----------");
System.out.println(money.format(calcTotalSales()));
}
}
public class Fahrenheit
{
public static void main (String[] args)
{
FahrenheitGUI converter = new FahrenheitGUI();
converter.display();
}
}
//********************************************************************
// FahrenheitGUI.java Author: Lewis/Loftus/Cocking
//
// Demonstrates the use of JFrame and JTextArea GUI components.
//********************************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FahrenheitGUI
{
private int WIDTH = 300;
private int HEIGHT = 75;
private JFrame frame;
private JPanel panel;
private JLabel inputLabel, outputLabel, resultLabel;
private JTextField fahrenheit;
//-----------------------------------------------------------------
// Sets up the GUI.
//-----------------------------------------------------------------
public FahrenheitGUI()
{
frame = new JFrame ("Temperature Conversion");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
inputLabel = new JLabel ("Enter Fahrenheit temperature:");
outputLabel = new JLabel ("Temperature in Celsius: ");
resultLabel = new JLabel ("---");
fahrenheit = new JTextField (5);
fahrenheit.addActionListener (new TempListener());
panel = new JPanel();
panel.setPreferredSize (new Dimension(WIDTH, HEIGHT));
panel.setBackground (Color.yellow);
panel.add (inputLabel);
panel.add (fahrenheit);
panel.add (outputLabel);
panel.add (resultLabel);
frame.getContentPane().add (panel);
}
//-----------------------------------------------------------------
// Displays the primary application frame.
//-----------------------------------------------------------------
public void display()
{
frame.pack();
frame.setVisible(true);
}
//*****************************************************************
// Represents an action listener for the temperature input field.
//*****************************************************************
private class TempListener implements ActionListener
{
//--------------------------------------------------------------
// Performs the conversion when the enter key is pressed in
// the text field.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
int fahrenheitTemp, celsiusTemp;
String text = fahrenheit.getText();
fahrenheitTemp = Integer.parseInt (text);
celsiusTemp = (fahrenheitTemp-32) * 5/9;
resultLabel.setText (Integer.toString (celsiusTemp));
}
}
}
public class PoundstoKilograms {
public static void main(String[] args) {
String title = new String("Weight conversion");
String label1 = new String("Pounds");
String label2 = new String("Kilograms");
PoundstoKilogramsGUI converter = new PoundstoKilogramsGUI(title,
label1, label2);
converter.display();
}
}