Exercise 4
Programmer: Mary Grace Cuming
Program Name: Name Echo
Date Started: March 16, 2009
Date Ended: March 19, 2009
Programm Purpose: A program that will ask a for the user name
And then print it back in a caps lock form.
import java.util.Scanner;
public class NameEcho{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
String username;
String fname;
String lname;
System.out.println("Enter your name:"); //ask for the user’s name name=scan.nextLine(); //get the name of the user using the scanner
int x=username.indexOf(" ");
fname=username.substring(0,x);
lname=username.substring(x);
System.out.println("\n%s %s\n",fname,lname.toUpperCase());
}
}
********************************************************
Exercise 5
Programmer: Mary Grace Cuming
Program Name: Hello Object Program
Date Started: March 13, 2009
Date Ended: March 18, 2009
Programm Purpose: to Manipulate a String
import java.util.Scanner;
public class helloObject { public static void main(String[] args)
{
String greetings; // variable used for the input string. Scanner input=new Scanner(System.in); // get the string given by the user System.out.println("Please Enter your Greeting:");
greetings=input.nextLine();
System.out.println();
System.out.println(greetings);
}
}
********************************************************
Exercise 1
Programmer: Mary Grace Cuming
Program Name: Word Reverser
Date Started: March 13, 2009
Date Ended: March 18, 2009
Programm Purpose: A program that will ask a
String to the user,then the program will read it and
print it in a reverse form.
import java.util.*;
public class reverseword{
public static void main(String args[])
{
String inputword;
String word;
Scanner scan=new Scanner(System.in);
System.out.println("Enter a sentence:");
input=scan.nextLine();
StringTokenizer st=new StringTokenizer(wordinput) ;
while (st.hasMoreTokens())
{ word=st.nextToken();
word=new StringBuffer(r).reverse().toString(); System.out.println(word+" ");
}
}
}
*************************************************************
Programmer: Mary Grace Cuming
Date Started: March 16, 2009
Date Ended: March 20, 2009
Program Purpose A program that used number buttons from 0-9, and once the user clicked three correct
import java.awt.*;import javax.swing.*;public class CombinationLock extends JFrame {public static void main (String [] args)
{ new CombinationLock().setVisible(true);
}public CombinationLock () { Container od = getContentPane(); od.setLayout(new FlowLayout()); od.add(new JButton("0")); od.add(new JButton("1")); od.add(new JButton("2")); od.add(new JButton("3")); od.add(new JButton("4")); od.add(new JButton("5")); od.add(new JButton("6")); od.add(new JButton("7")); od.add(new JButton("8")); od.add(new JButton("9")); od setBackground(Color.pink); pack(); }}
Friday, March 20, 2009
Wednesday, March 11, 2009
/**
Programmer: Cuming Mary Grace
Program name: User-Friendly Division
Date Started:MArch 2,2009
Date Finished: MArch 11, 2009
Purpose of the programm: To learn more about Exception Handling in a program
*/
import java.util.InputMismatchException;
import java.util.Scanner;
public class DivisionPractice
{
throws ArithmeticException
{
return numerator / denominator; // possible division by zero
public static void main( String args[] )
{
Scanner scanner = new Scanner( System.in ); // scanner for input
boolean continueLoop = true; // determines if more input is needed
//the try block
do
{
System.out.print( "Please enter an integer numerator: " );
int numerator = scanner.nextInt();
System.out.print( "Please enter an integer denominator: " );
int denominator = scanner.nextInt();
int result = quotient( numerator, denominator );
System.out.printf( "\nResult: %d / %d = %d\n", numerator,
denominator, result );
continueLoop = false; // input successful; end looping
} // end try
catch ( InputMismatchException inputMismatchException )
{
System.err.printf( "\nException: %s\n",
inputMismatchException );
scanner.nextLine(); // discard input so user can try again
System.out.println(
"You must enter integers. Please try again.\n" );
} // end catch
catch ( ArithmeticException arithmeticException )
{
System.err.printf( "\nException: %s\n", arithmeticException );
System.out.println(
"Zero is an invalid denominator. Please try again.\n" );
} // end catch
} while ( continueLoop ); // end do...while
} // end main
} // end class DivideByZeroWithExceptionHandling
Programmer: Cuming Mary Grace
Program name: User-Friendly Division
Date Started:MArch 2,2009
Date Finished: MArch 11, 2009
Purpose of the programm: To learn more about Exception Handling in a program
*/
import java.util.InputMismatchException;
import java.util.Scanner;
public class DivisionPractice
{
// demonstrates throwing an exception using the Arithmetic Exception when a divide-by-zero occurs
throws ArithmeticException
{
return numerator / denominator; // possible division by zero
} // end method quotient
public static void main( String args[] )
{
Scanner scanner = new Scanner( System.in ); // scanner for input
boolean continueLoop = true; // determines if more input is needed
//the try block
do
{
{
System.out.print( "Please enter an integer numerator: " );
int numerator = scanner.nextInt();
System.out.print( "Please enter an integer denominator: " );
int denominator = scanner.nextInt();
int result = quotient( numerator, denominator );
System.out.printf( "\nResult: %d / %d = %d\n", numerator,
denominator, result );
continueLoop = false; // input successful; end looping
} // end try
catch ( InputMismatchException inputMismatchException )
{
System.err.printf( "\nException: %s\n",
inputMismatchException );
scanner.nextLine(); // discard input so user can try again
System.out.println(
"You must enter integers. Please try again.\n" );
} // end catch
catch ( ArithmeticException arithmeticException )
{
System.err.printf( "\nException: %s\n", arithmeticException );
System.out.println(
"Zero is an invalid denominator. Please try again.\n" );
} // end catch
} while ( continueLoop ); // end do...while
} // end main
} // end class DivideByZeroWithExceptionHandling
Tuesday, March 10, 2009
/**
Programmer: Cuming Mary Grace
Program name: Arraylist(Iterator)
Date Started:MArch 2,2009
Date Finished: MArch 11, 2009
Purpose of the programm: To know more about Iterator
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class IteratorSample {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList(); // this create an Arraylist for the Animal
arrayList.add("cat"); //it Add elements to Arraylist
arrayList.add("dog");
arrayList.add("rat");
arrayList.add("bat");
arrayList.add("bug");
//in order to get the object in the arraylist we used the Iterator() method.
ListIterator itr = arrayList.listIterator();
//use hasNext() which Returns true if the iteration has more elements
//and next()method which Returns the next element in the iteration in order to iterate through the elements
System.out.println("Animal ArrayList elements...");
while(itr.hasNext())
System.out.println(itr.next());
/*
Use hasPrevious() and previous() methods of ListIterator to iterate through
the elements in backward direction.
*/
System.out.println("Iterating through ArrayList elements in backward direction...");
while(itr.hasPrevious())
System.out.println(itr.previous());
}
}
Programmer: Cuming Mary Grace
Program name: Arraylist(Iterator)
Date Started:MArch 2,2009
Date Finished: MArch 11, 2009
Purpose of the programm: To know more about Iterator
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class IteratorSample {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList(); // this create an Arraylist for the Animal
arrayList.add("cat"); //it Add elements to Arraylist
arrayList.add("dog");
arrayList.add("rat");
arrayList.add("bat");
arrayList.add("bug");
//in order to get the object in the arraylist we used the Iterator() method.
ListIterator itr = arrayList.listIterator();
//use hasNext() which Returns true if the iteration has more elements
//and next()method which Returns the next element in the iteration in order to iterate through the elements
System.out.println("Animal ArrayList elements...");
while(itr.hasNext())
System.out.println(itr.next());
/*
Use hasPrevious() and previous() methods of ListIterator to iterate through
the elements in backward direction.
*/
System.out.println("Iterating through ArrayList elements in backward direction...");
while(itr.hasPrevious())
System.out.println(itr.previous());
}
}
Sunday, February 8, 2009
Direct clothing Case Study
/*Programmer:Mary Grace Cuming
Date Started:February 7,2009
Date Ended:February 8,2009 Program name:Direct Clothing CAse Study Solution(Class Shirt) */
public class Shirt {
private String shirtID; private double price; private String color; private int quantity; private String description;
public Shirt(){ }
public Shirt(String newshirtID,double newprice,String newcolor, int newquantity,String newdescription){ //constructor
shirtID= newshirtID; price=newprice; color= newcolor; quantity=newquantity; description=newdescription;}
//method add stockpublic void setShirtID(String newshirtID){ shirtID= newshirtID;}
public String getShirtID(){ return shirtID;} public void setPrice(double newprice){ price=newprice;}public double getPrice(){ return price;}
public void setColor(String newcolor){ color= newcolor;}public String getColor(){ return color;}
public void setQuantity(int newquantity){ quantity=newquantity;}public int getQuantity(){ return quantity;}public void setDescription(String newdescription){ description=newdescription;}public String getDescription(){ return description;}public String("
public String removestock(){ System.out.println("The Shirt with an ID\t\s getShirtID()); System.out.println("was successfully remove.Thank you!!!");}
}
________________________________________________________________
/*Programmer:Mary Grace Cuming
Date Started:February 7,2009
Date Ended:February 8,2009 Program name:Direct Clothing CAse Study Solution(Class Order) */
public class Order{ private String orderID; private double totalprice; private String status;
public Order(){}
public void orderShirt(String neworderID,double newtotalorderprice,String newstatus){ String orderID=neworderID; double totalorderprice=newtotalorderprice; String status=newstatus;}public void setOrderID(String neworderID){ String orderID=neworderID;}public String getOrderID(){ return orderID;}public void setTotaltorderprice(double newtotalorderprice){ double totalorderprice=newtotalorderprice;}public double getTotaltorderprice(){ return totalorderprice;}public void setStatus(String newstatus){ String status=newstatus;}public String getStatus(){ return status;}}
_____________________________________________________________
/*Programmer:Mary Grace Cuming
Date Started:February 7,2009
Date Ended:February 8,2009 Program name:Direct Clothing CAse Study Solution(Class FormofPayment) */
public class FormofPayment{ private String checknumber; private String cardnumber; private String expirationDate;
public FormofPayment(String check,String card,String date)
//constructor{
checknumber=check; cardnumber=card; expirationDate=date }
//methodpublic void setChecknumber(String check){ checknumber=check;} public String getChecknumber(){ return checknumber;}
public void setCardnumber(String card){ cardnumber=card;} public String getCardnumber(){ return cardnumber;}
public void setExpirationdate(String date){ expirationDate=date;} public String getExpirationdate(){ returnexpirationDate;}public void Dispalymessage(){ System.out.println("Your checknumber is:%s/nand checknumber is:%s");
}
}
__________________________________________________________
/*Programmer:Mary Grace Cuming
Date Started:February 7,2009
Date Ended:February 8,2009 Program name:Direct Clothing CAse Study Solution(Class Catalog) */
public class Catalog {
public void displayCatalog(){ System.out.println("ShirtID:252-22\nPrice:25,000\nColor:Black \nQuantity:5\nDescription:Beautiful"); System.out.println("ShirtID:252-54\nPrice:90,000\nColor:Pink\nQuantity:1\nDescription:Nice");}}
____________________________________________________________
/*Programmer:Mary Grace Cuming
Date Started:February 7,2009
Date Ended:February 8,2009 Program name:Direct Clothing CAse Study Solution(Class Order) */
public class Order{ private String orderID; private double totalprice; private String status;
public Order(){}
public void orderShirt(String neworderID,double newtotalorderprice,String newstatus){ String orderID=neworderID; double totalorderprice=newtotalorderprice; String status=newstatus;}public void setOrderID(String neworderID){ String orderID=neworderID;}public String getOrderID(){ return orderID;}public void setTotaltorderprice(double newtotalorderprice){ double totalorderprice=newtotalorderprice;}public double getTotaltorderprice(){ return totalorderprice;}public void setStatus(String newstatus){ String status=newstatus;}public String getStatus(){ return status;}}
____________________________________________________________
/*Programmer:Mary Grace Cuming
Date Started:February 7,2009
Date Ended:February 8,2009
Program name:Direct Clothing CAse Study Solution(Class ClothingTester) */
import java.util.Scanner;
public class ClothingTester{ public static void main(String args[]) { Scanner input= new Scanner(System.in); Shirt myShirt=new Shirt(); System.out.println("Welcome to Direct Clothing:\n\nHere's our Catalog?"); myShirt.displayCatalog(); Scanner input= new Scanner(System.in); System.out.println("Want to add a stock?"); if (input=='y') { System.out.println("Enter ShirtID:"); String ID=input.nextLine(); myShirt.setShirtID(ID); System.out.println(); System.out.println("Enter Price:"); double cprice=input.nextDouble(); myShirt.setPrice(cprice); System.out.println();
System.out.println("Enter Color:"); String ccolor=input.nextLine(); myShirt.setColor(ccolor); System.out.println(); System.out.println("Enter Quantity:"); int cquantity=input.nextInt(); myShirt.setQuantity(cquantity); System.out.println(); System.out.println("Enter shirt Description:"); String cdescription=input.nextLine(); myShirt.setDescription(cdescription); System.out.println(); System.out.println("The shirt was Successfully Added.\n\n\t Thank You!!!"); } else { System.out.println("Ok"); } } }
Date Started:February 7,2009
Date Ended:February 8,2009 Program name:Direct Clothing CAse Study Solution(Class Shirt) */
public class Shirt {
private String shirtID; private double price; private String color; private int quantity; private String description;
public Shirt(){ }
public Shirt(String newshirtID,double newprice,String newcolor, int newquantity,String newdescription){ //constructor
shirtID= newshirtID; price=newprice; color= newcolor; quantity=newquantity; description=newdescription;}
//method add stockpublic void setShirtID(String newshirtID){ shirtID= newshirtID;}
public String getShirtID(){ return shirtID;} public void setPrice(double newprice){ price=newprice;}public double getPrice(){ return price;}
public void setColor(String newcolor){ color= newcolor;}public String getColor(){ return color;}
public void setQuantity(int newquantity){ quantity=newquantity;}public int getQuantity(){ return quantity;}public void setDescription(String newdescription){ description=newdescription;}public String getDescription(){ return description;}public String("
public String removestock(){ System.out.println("The Shirt with an ID\t\s getShirtID()); System.out.println("was successfully remove.Thank you!!!");}
}
________________________________________________________________
/*Programmer:Mary Grace Cuming
Date Started:February 7,2009
Date Ended:February 8,2009 Program name:Direct Clothing CAse Study Solution(Class Order) */
public class Order{ private String orderID; private double totalprice; private String status;
public Order(){}
public void orderShirt(String neworderID,double newtotalorderprice,String newstatus){ String orderID=neworderID; double totalorderprice=newtotalorderprice; String status=newstatus;}public void setOrderID(String neworderID){ String orderID=neworderID;}public String getOrderID(){ return orderID;}public void setTotaltorderprice(double newtotalorderprice){ double totalorderprice=newtotalorderprice;}public double getTotaltorderprice(){ return totalorderprice;}public void setStatus(String newstatus){ String status=newstatus;}public String getStatus(){ return status;}}
_____________________________________________________________
/*Programmer:Mary Grace Cuming
Date Started:February 7,2009
Date Ended:February 8,2009 Program name:Direct Clothing CAse Study Solution(Class FormofPayment) */
public class FormofPayment{ private String checknumber; private String cardnumber; private String expirationDate;
public FormofPayment(String check,String card,String date)
//constructor{
checknumber=check; cardnumber=card; expirationDate=date }
//methodpublic void setChecknumber(String check){ checknumber=check;} public String getChecknumber(){ return checknumber;}
public void setCardnumber(String card){ cardnumber=card;} public String getCardnumber(){ return cardnumber;}
public void setExpirationdate(String date){ expirationDate=date;} public String getExpirationdate(){ returnexpirationDate;}public void Dispalymessage(){ System.out.println("Your checknumber is:%s/nand checknumber is:%s");
}
}
__________________________________________________________
/*Programmer:Mary Grace Cuming
Date Started:February 7,2009
Date Ended:February 8,2009 Program name:Direct Clothing CAse Study Solution(Class Catalog) */
public class Catalog {
public void displayCatalog(){ System.out.println("ShirtID:252-22\nPrice:25,000\nColor:Black \nQuantity:5\nDescription:Beautiful"); System.out.println("ShirtID:252-54\nPrice:90,000\nColor:Pink\nQuantity:1\nDescription:Nice");}}
____________________________________________________________
/*Programmer:Mary Grace Cuming
Date Started:February 7,2009
Date Ended:February 8,2009 Program name:Direct Clothing CAse Study Solution(Class Order) */
public class Order{ private String orderID; private double totalprice; private String status;
public Order(){}
public void orderShirt(String neworderID,double newtotalorderprice,String newstatus){ String orderID=neworderID; double totalorderprice=newtotalorderprice; String status=newstatus;}public void setOrderID(String neworderID){ String orderID=neworderID;}public String getOrderID(){ return orderID;}public void setTotaltorderprice(double newtotalorderprice){ double totalorderprice=newtotalorderprice;}public double getTotaltorderprice(){ return totalorderprice;}public void setStatus(String newstatus){ String status=newstatus;}public String getStatus(){ return status;}}
____________________________________________________________
/*Programmer:Mary Grace Cuming
Date Started:February 7,2009
Date Ended:February 8,2009
Program name:Direct Clothing CAse Study Solution(Class ClothingTester) */
import java.util.Scanner;
public class ClothingTester{ public static void main(String args[]) { Scanner input= new Scanner(System.in); Shirt myShirt=new Shirt(); System.out.println("Welcome to Direct Clothing:\n\nHere's our Catalog?"); myShirt.displayCatalog(); Scanner input= new Scanner(System.in); System.out.println("Want to add a stock?"); if (input=='y') { System.out.println("Enter ShirtID:"); String ID=input.nextLine(); myShirt.setShirtID(ID); System.out.println(); System.out.println("Enter Price:"); double cprice=input.nextDouble(); myShirt.setPrice(cprice); System.out.println();
System.out.println("Enter Color:"); String ccolor=input.nextLine(); myShirt.setColor(ccolor); System.out.println(); System.out.println("Enter Quantity:"); int cquantity=input.nextInt(); myShirt.setQuantity(cquantity); System.out.println(); System.out.println("Enter shirt Description:"); String cdescription=input.nextLine(); myShirt.setDescription(cdescription); System.out.println(); System.out.println("The shirt was Successfully Added.\n\n\t Thank You!!!"); } else { System.out.println("Ok"); } } }
Wednesday, February 4, 2009
/* programmer:Cuming Mary Grace
date started: February 4, 2009
date ended: February 4, 2009
Programme name: Solving a Cube
programme purpose: To create classes using Visibility access Modifiers
public class Cube{
private double width;
private double length;
private double height;
private double volume;
private double area;
// constructor
public Cube(double w, double h, double l)
{
width=w;
height=h;
length=l;
}
public Cube()
{
}
private double volume() //method for the volume
{
return (width*length*height);
}
private double area() //method for the area
{
return (width*length);
}
public void setDimension(double nlength, double nHeight, double nWidth)
{
length=nLength;
width=nWidth;
height=nHeight;
}
public void displayCube()
{
System.out.println("The volume of the Cube:" +volume());
System.out.println("The area of the Cube:" +area());
}
}
CubeTester
/* programmer:Cuming Mary Grace
date started: February 4, 2009
date ended: February 4, 2009
Programme name:Solving a Cube
programme purpose: To create classes using Visibility access Modifiers
import java.util.Scanner;
class CubeTester
{
public static void main(String args[])
{
double l;
double w;
double h;
System.out.println("The Cube with a parameter");
Cube newCube=new Cube(1,2,3);;
newCube.displayCube();
System.out.println("The Cube object without a parameter");
Scanner a=new Scanner(System.in);
System.out.println("enter the value of the length:");
l=a.nextDouble();
Scanner b=new Scanner(System.in);
System.out.println("enter the value of the height:");
w=b.nextDouble();
Scanner c=new Scanner(System.in);
System.out.println("enter the value of the width:");
h=c.nextDouble();
System.out.println("The Cube object without a parameter");
Cube secondCube=new Cube();
secondCube.setDimension(l,w,h);
secondCube.displayCube();
}
}
date started: February 4, 2009
date ended: February 4, 2009
Programme name: Solving a Cube
programme purpose: To create classes using Visibility access Modifiers
public class Cube{
private double width;
private double length;
private double height;
private double volume;
private double area;
// constructor
public Cube(double w, double h, double l)
{
width=w;
height=h;
length=l;
}
public Cube()
{
}
private double volume() //method for the volume
{
return (width*length*height);
}
private double area() //method for the area
{
return (width*length);
}
public void setDimension(double nlength, double nHeight, double nWidth)
{
length=nLength;
width=nWidth;
height=nHeight;
}
public void displayCube()
{
System.out.println("The volume of the Cube:" +volume());
System.out.println("The area of the Cube:" +area());
}
}
CubeTester
/* programmer:Cuming Mary Grace
date started: February 4, 2009
date ended: February 4, 2009
Programme name:Solving a Cube
programme purpose: To create classes using Visibility access Modifiers
import java.util.Scanner;
class CubeTester
{
public static void main(String args[])
{
double l;
double w;
double h;
System.out.println("The Cube with a parameter");
Cube newCube=new Cube(1,2,3);;
newCube.displayCube();
System.out.println("The Cube object without a parameter");
Scanner a=new Scanner(System.in);
System.out.println("enter the value of the length:");
l=a.nextDouble();
Scanner b=new Scanner(System.in);
System.out.println("enter the value of the height:");
w=b.nextDouble();
Scanner c=new Scanner(System.in);
System.out.println("enter the value of the width:");
h=c.nextDouble();
System.out.println("The Cube object without a parameter");
Cube secondCube=new Cube();
secondCube.setDimension(l,w,h);
secondCube.displayCube();
}
}
Subscribe to:
Posts (Atom)