Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
CSE 292 Java Programming
Professor Tong Lai Yu
Homework 2
Santiago Campero
1) The programs Account.java and AccountTest.java are from your
textbook. As stated in AccountTest.java, write the code to
withdraw money from account.
Program Run
account1 balance: $50.00
account2 balance: $0.00
Enter withdrawal amount for account1: 34
subtracting 34.00 from account1 balance
account1 balance: $16.00
account2 balance: $0.00
Enter withdrawal amount for account2: 3
subtracting 3.00 from account2 balance
No sufficient funds, you have only $ 0.0
account1 balance: $16.00
account2 balance: $0.00
Account.java
// Account.java
// Account class with a constructor to
// initialize instance variable balance.
package edu.CSE292.HW2a;
public class Account
{
private double balance; // instance variable that stores the balance
// constructor
public Account( double initialBalance )
{
// validate that initialBalance is greater than 0.0;
// if it is not, balance is initialized to the default value 0.0
if ( initialBalance > 0.0 )
balance = initialBalance;
} // end Account constructor
// credit (add) an amount to the account
public void credit( double amount )
{
balance = balance + amount; // add amount to balance
} // end method credit
/* write code to declare method debit */
public void withdraw(double withdrawAmount)
{
if(withdrawAmount <= balance)
{
balance = balance - withdrawAmount;
}
else
System.out.println("No sufficient funds, you have only $ " + balance);
}
// return the account balance
public double getBalance()
{
return balance; // gives the value of balance to the calling method
} // end method getBalance
} // end class Account
AccountTest.java
//Lab 1: AccountTest.java
//Create and manipulate an Account object.
package edu.CSE292.HW2a;
import java.util.Scanner;
public class AccountTest
{
// main method begins execution of Java application
public static void main( String args[] )
{
Account account1 = new Account( 50.00 ); // create Account object
Account account2 = new Account( -7.53 ); // create Account object
// display initial balance of each object
System.out.printf( "account1 balance: $%.2f\n",
account1.getBalance() );
System.out.printf( "account2 balance: $%.2f\n\n",
account2.getBalance() );
// create Scanner to obtain input from command window
@SuppressWarnings("resource")Scanner input = new Scanner( System.in );
double withdrawalAmount; // withdrawal amount read from user
System.out.print( "Enter withdrawal amount for account1: " );
withdrawalAmount = input.nextDouble(); // obtain user input
System.out.printf( "\nsubtracting %.2f from account1
balance\n",withdrawalAmount);
/* write code to withdraw money from account */
account1.withdraw(withdrawalAmount);
// display balances
System.out.printf( "account1 balance: $%.2f\n",
account1.getBalance() );
System.out.printf( "account2 balance: $%.2f\n\n",
account2.getBalance() );
System.out.print( "Enter withdrawal amount for account2: " );
withdrawalAmount = input.nextDouble(); // obtain user input
System.out.printf( "\nsubtracting %.2f from account2 balance\n",
withdrawalAmount );
/* write code to withdraw from account */
account2.withdraw(withdrawalAmount);
// display balances
System.out.printf( "account1 balance: $%.2f\n",
account1.getBalance() );
System.out.printf( "account2 balance: $%.2f\n",
account2.getBalance() );
} // end main
} // end class AccountTest
2) Program templates Palindrome.java and PalindromeTest.java are
provided.Write the code as stated in Palindrome.java and test the
code using PalindromeTest.java
Program Runs
Enter five digit number:
12321
12321 is a palindrome
Enter five digit number:
12345
12345 is not a palindrome
Palindrome.java
//Lab 2: Palindrome.java
//Program tests for a palindrome
package edu.CSE292.HW2b;
import java.util.Scanner;
public class Palindrome
{
// checks if a 5-digit number is a palindrome
public void checkPalindrome()
{
Scanner input = new Scanner( System.in );
int number; // user input number
int digit1; // first digit
int digit2; // second digit
int digit3;
int digit4; // fourth digit
int digit5; // fifth digit
int digits; // number of digits in input
number = 0;
digits = 0;
/* Write code that inputs a five-digit number. Display an error message
if the number is not five digits. Loop until a valid input is received. */
System.out.println( "Enter five digit number: " ); // prompt
number = input.nextInt(); // read number
while (number < 10000 && number > 99999)
{
System.out.println("Incorrect input, please try again...");
System.out.println( "Enter five digit number: " ); // prompt
number = input.nextInt(); // read number
}
/* Write code that separates the digits in the five digit number. Use
division to isolate the left-most digit in the number, use a remainder
calculation to remove that digit from the number. Then repeat this
process. */
digit1 = number / 10000;
digit2 = number / 1000 % 10;
digit3 = number % 1000 / 100 % 10;
digit4 = number % 10000 % 1000 % 100 / 10;
digit5 = number % 10000 % 1000 % 100 % 10;
/* Write code that determines whether the first and last digits are
identical and the second and Fourth digits are identical. Output
whether or not the original string is a palindrome. */
if (digit1 == digit5 && digit2 == digit4)
System.out.printf("%d is a palindrome", number);
else
System.out.printf("%d is not a palindrome", number);
} // end method checkPalindrome
} // end class Palindrome
PalindromeTest.java
//Lab 2: PalindromeTest.java
//Test application for class Palindrome
package edu.CSE292.HW2b;
public class PalindromeTest
{
public static void main( String args[] )
{
Palindrome application = new Palindrome();
application.checkPalindrome();
} // end main
} // end class PalindromeTest
3)Program templates Largest.java and LargestTest.java are provided.
Write the code as stated in Largest.java and test the code using LargestTest.java
Program Run
1
2
The
3
The
4
The
5
The
9
number entered is 2
number entered is 3
number entered is 4
number entered is 5
The number entered is
8
The number entered is
7
The number entered is
5
The number entered is
7
The number entered is
8
The number entered is
Largest number is 9
9
8
7
5
7
8
Largest.java
//Lab 3: Largest.java
//Program determines and prints the largest of ten numbers.
package edu.CSE292.HW2c;
import java.util.Scanner;
public class Largest
{
// determine the largest of 10 numbers
public void determineLargest()
{
Scanner input = new Scanner( System.in );
int largest; // largest number
int number; // user input
int counter; // number of values entered
/* write code to get the first integer and store it in variable largest */
number = input.nextInt();
largest =number;
/* write code to initialize the number of integers entered */
counter =0;
/* write code to loop until 10 numbers are entered */
/* write code to prompt the user to enter a number and read that number */
/* write code to test whether the number entered is greater than the largest
if so, replace the value of largest with the entered number */
/* write code to increment the number of integers entered */
while (counter < 10){
number = input.nextInt();
System.out.printf( "The number entered is %d\n", number );
if (largest < number)
largest = number;
counter++;
}
System.out.printf( "Largest number is %d\n", largest );
} // end method determineLargest
} // end class Largest
LargestTest.java
//Lab 3: LargestTest.java
//Test application for class Largest
package edu.CSE292.HW2c;
public class LargestTest
{
public static void main( String args[] )
{
Largest application = new Largest();
application.determineLargest();
} // end main
} // end class LargestTest
4) Modify DrawPanel.java of Fig. 4.18 to draw the following:
a triangle
a circle
a pentagon
a filled rectangle
an ellipse
Program Run
DrawPanel.java
// Lab4
// Fig. 4.18: DrawPanel.java
// Using drawLine to connect the corners of a panel.
package edu.CSE292.HW2d;
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawPanel extends JPanel
{
// draws an X from the corners of the panel
public void paintComponent( Graphics g )
{
// call paintComponent to ensure the panel displays correctly
super.paintComponent( g );
int width = getWidth(); // total width
int height = getHeight(); // total height
// draw a line for triangle
g.drawLine( 0, 0, width/10, 0 );
g.drawLine(0, 0, 0, width/10);
g.drawLine( 0, height/10, width/10, 0 );
// draw a circle
g.drawOval(0, 0, width, height);
// draw a polygon pentagon
int Cursor;
int[] XArray = {80, 40, 110, 180, 160 };
int[] YArray = {20, 70, 120, 70, 20};
g.drawPolygon (XArray, YArray, 5);
Cursor = 0;
while (Cursor < 5) {
XArray [Cursor] = XArray [Cursor] + 200;
Cursor = Cursor + 1;
}
// draw filled rectangle
g.drawRect(90, 40, 50, 30);
g.fillRect(90, 40, 50, 30);
// draw ellipse
g.drawOval(100, 150, width/8, height/4);
} // end method paintComponent
} // end class DrawPanel
DrawPanelTest.java
//Fig. 4.19: DrawPanelTest.java
// Application to display a DrawPanel.
package edu.CSE292.HW2d;
import javax.swing.JFrame;
public class DrawPanelTest
{
public static void main( String[] args )
{
// create a panel that contains our drawing
DrawPanel panel = new DrawPanel();
// create a new frame to hold the panel
JFrame application = new JFrame();
// set the frame to exit when it is closed
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
application.add( panel ); // add the panel to the frame
application.setSize( 250, 250 ); // set the size of the frame
application.setVisible( true ); // make the frame visible
} // end main
} // end class DrawPanelTest