Programming Lab 2 : classes, objects, data types and input/output
CSCI-UA 9102, Data Structures
public class Circle{
public static void main(String [] args){
// 1. start by declaring your variables
// 2. Display the sentence "Value of radius: " on the screen
// 3. Read the input value and store it in the radius variable
// 4. Compute the perimeter using the appropriate formula
/* 5. display the result using the sentence "The circle with radius "
+ your value of the radius +
"has as perimeter: "+ the perimeter you just computed */
}
}
public static int f(int x){
int resultat;
resultat = -x*x + 3*x - 2;
return resultat;
}
resize(double x)
) that resizes a shape’s dimension by a factor x.
//********************************************************************
// Coin.java Author: Lewis/Loftus
//
// Represents a coin with two sides that can be flipped.
//********************************************************************
public class Coin
{
private final int HEADS = 0;
private final int TAILS = 1;
private int face;
//-----------------------------------------------------------------
// Sets up the coin by flipping it initially.
//-----------------------------------------------------------------
public Coin()
{
flip();
}
//-----------------------------------------------------------------
// Flips the coin by randomly choosing a face value.
//-----------------------------------------------------------------
public void flip()
{
face = (int) (Math.random() * 2);
}
//-----------------------------------------------------------------
// Returns true if the current face of the coin is heads.
//-----------------------------------------------------------------
public boolean isHeads()
{
return (face == HEADS);
}
//-----------------------------------------------------------------
// Returns the current face of the coin as a string.
//-----------------------------------------------------------------
public String toString()
{
String faceName;
if (face == HEADS)
faceName = "Heads";
else
faceName = "Tails";
return faceName;
}
}
Snowman.java
given below. Create a revised version of the Snowman applet with the following modifications:
//********************************************************************
// Snowman.java Author: Lewis/Loftus
//
// Demonstrates basic drawing methods and the use of color.
//********************************************************************
import javax.swing.*;
import java.awt.*;
public class Snowman
{
public static void main(String[] args)
{
SnowmanFrame frame = new SnowmanFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
A frame that contains a message panel
*/
class SnowmanFrame extends JFrame
{
public SnowmanFrame()
{
setTitle("Snowman");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
SnowmanPanel panel = new SnowmanPanel();
getContentPane().add(panel);
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 400;
}
class SnowmanPanel extends JPanel
{
//-----------------------------------------------------------------
// Draws a snowman.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent(page);
final int MID = 150;
final int TOP = 50;
setBackground (Color.cyan);
page.setColor (Color.blue);
page.fillRect (0, 175, 300, 50); // ground
page.setColor (Color.yellow);
page.fillOval (-40, -40, 80, 80); // sun
page.setColor (Color.white);
page.fillOval (MID-20, TOP, 40, 40); // head
page.fillOval (MID-35, TOP+35, 70, 50); // upper torso
page.fillOval (MID-50, TOP+80, 100, 60); // lower torso
page.setColor (Color.black);
page.fillOval (MID-10, TOP+10, 5, 5); // left eye
page.fillOval (MID+5, TOP+10, 5, 5); // right eye
page.drawArc (MID-10, TOP+20, 20, 10, 190, 160); // smile
page.drawLine (MID-25, TOP+60, MID-50, TOP+40); // left arm
page.drawLine (MID+25, TOP+60, MID+55, TOP+60); // right arm
page.drawLine (MID-20, TOP+5, MID+20, TOP+5); // brim of hat
page.fillRect (MID-15, TOP-20, 30, 25); // top of hat
}
}
Snowman
class, write an applet that draws a house with a door (and doorknob), windows, and a chimney. Add some smoke coming out of the chimney and some clouds in the sky
Shape
numSides
numSides
getArea()
, getPerimeter()
Rectangle
width
, height
.
width
, height
.main
method to define a Rectangle and a Triangle.
MyDate
class should satisfy the following specifications: It should contain:
year
, month
and day
. month
is 0-based (i.e. 0 is for January) MyDate
object for the current dateMyDate
object with specified year, month and daysetDate(long elapsedTime)
that sets a new date for the object using the elapsed time.Account
class that should be designed as follows. The class should contain:
int
variable named id
for the account (default 0)
double
variable named balance
for the account (default 0)
double
variable named annualInterestRate
that stores the current interest rate (default 0). We assume all accounts have the same interest rate
Date
variable named dateCreated
that stores the date when the account was created.
id
, balance
and annualInterestRate
dateCreated
getMonthlyInterestRate()
that returns the monthly interest rate.
getMonthlyInterest()
that returns the monthly interest
withdraw
that withdraw a specified amount from the account
deposit
that deposits a specified amount to the account.
string
description of the account.
T
, and supporting a single constructor that accepts an
initial value. Make all corresponding modifications to the rest of the classes in
our hierarchy so that they remain as nongeneric classes, while inheriting from the
new generic Progression class
import java.util.Scanner;
public class PalindromeTester{
// -----------------------------------------------
// Test strings to see if they are palindromes
// -----------------------------------------------
public static void main(String [] args){
String str, another = "y";
int left; right;
Scanner scan = new Scanner(System.in);
while(another.equalsIgnoreCase("y")) // allows y or Y{
System.out.println("Enter a potential palindrome: ");
str = scan.nextLine();
left = 0;
right = str.length()-1;
while(str.charAt(left) == str.charAt(right) && left < right){
left++;
right--; }
System.out.println();
if(left < right)
System.out.println("That string is NOT a palindrome");
else
System.out.println("That string IS a palindrome");
System.out.println();
System.out.print("Test another palindrome (y/n)? ");
another = scan.nextLine();
}}}