d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Need Help Using Java > 3 Problems
Add Reply New Topic New Poll
Member
Posts: 4,635
Joined: Aug 17 2011
Gold: 27,309.41
Apr 16 2013 12:37pm
Alright so I have Java HW where I need to make some programs.

If you can get anyone of these HW problems to work, I will greatly scratch your back to reward the help!!!

PM me for any additional details or questions you have B) . LMK if you are able to be of service :)


Here are the 3 questions:

1. Coin Toss Simulator
Write a class named Coin. The Coin class should have the following field:
• A String named sideUp. The sideUp field will hold either “heads” or “tails” indicating the side of the coin that is facing up.
The Coin class should have the following methods:
• A no-arg constructor that randomly determines the side of the coin that is
facing up (“heads” or “tails”) and initializes the sideUp field accordingly.
• A void method named toss that simulates the tossing of the coin. When the
toss method is called, it randomly determines the side of the coin that is facing
up (“heads” or “tails”) and sets the sideUp field accordingly.
• A method named getSideUp that returns the value of the sideUp field.

Write a program that demonstrates the Coin class. The program should create an instance
of the class and display the side that is initially facing up. Then, use a loop to toss the
coin 20 times. Each time the coin it tossed, display the side that is facing up. The program
should keep count of the number of times heads is facing up and the number of times tails
is facing up, and display those values after the loop finishes.





2. Complex class
Create a class called Complex for performing arithmetic with complex numbers.
Complex numbers have the form
realPart + imaginaryPart * i (e.g., a + bi)
where i is : 1
The class should contain:
• Two double data fields x and y that represent the real and imaginary part of a
complex number.
• A no-arg constructor that creates a complex number (0 + 0i).
• A constructor that creates a Complex object with specified real and
imaginary part of the complex number.
• Two get methods for data fields x and y, respectively.
• A method named add that returns the addition of this complex number with
another. Complex numbers are added by adding the real and imaginary parts
of the summands. That is to say:
(a+bi) + (c+di) = (a+c) + (b+d)i
The method is: public Complex add(Complex c) {}
• A method named subtract that returns the subtraction of this complex
number with another. Subtraction is defined by:
(a+bi) + (c+di) = (a-c) + (b-d)i
The method is: public Complex subtract(Complex c) {}
• A method named multiply that returns the multiplication of this complex
with another. The multiplication of two complex numbers is defined by the
following formula:
( a+bi )( c+di ) = ( ac – bd ) + ( bc + ad )i
The method is: public Complex multiply(Complex c) {}
• A toString() method that prints a Complex object in the form (a)+(b)i, where
a is the real part and b is the imaginary part.


3. FlowerCounter
A flower stand has ten kinds of flowers – petunia, pansy, rose, violet, carnation,
lily, poppy, daisy, tulip, and hydrangea are stocked and cost, respectively, 50¢,
75¢, $1.50, 75¢, 85¢, $1.25, $2.15, 85¢, $2.75, and $1.30 per flower.
Create a class called Flowers that contains:
• A string data field named name for flower name.
• A double field named price for the flower price.
• A constructor that constructs a Flowers object with a specified name and
price.
• A method named getName() that returns the name of the flower.
• A method named getPrice() that returns the price of the flower.
Write a program named FlowerCounter that contains an array of 10 Flowers
objects and fill in it with the data given above accordingly. In your program, the
user should read the name of a flower and the quantity desired by a customer.
Locate the flower name in the array and use that index to find the cost per stem.
Compute and print each kind of the flower’s cost and the total cost of the sale. In
your program, a message should be displayed if the user enters a flower name
that doesn’t include on the list
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Apr 16 2013 01:00pm
Coin toss simulator

'Coin' Class:

Code
public class Coin {
protected String sideUp;

public Coin() {
 if(Math.random() < 0.5)
  this.sideUp = "heads";
 else
  this.sideUp = "tails";
}

public void toss() {
 if(Math.random() < 0.5)
  this.sideUp = "heads";
 else
  this.sideUp = "tails";
}

public String getSideUp() {
 return this.sideUp;
}
}


main Class:

Code
public class run {
public static void main(String[] args) {
 Coin myCoin = new Coin();
 System.out.println("Coin initially is " + myCoin.getSideUp());
 for(int i = 0; i < 20; i++) {
  myCoin.toss();
  System.out.println("Coin is now " + myCoin.getSideUp());
 }
}
}
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Apr 16 2013 07:05pm
I got most of the FlowerCounter assignment done for you while I was at work today. If you still need it, I'll send it to you tomorrow morning, and I'll do the complex class one too.
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Apr 17 2013 07:13am
Flower.java
Code
public class Flower {

 /*
  Create a class called Flowers that contains:
• A string data field named name for flower name.
• A double field named price for the flower price.
• A constructor that constructs a Flowers object with a specified name and
price.
• A method named getName() that returns the name of the flower.
• A method named getPrice() that returns the price of the flower.
  */
 private String name;
 
 private double price;

 public Flower( String name, double price ) {
   super( );
   this.name = name;
   this.price = price;
 }

 public String getName( ) {
   return name;
 }

 public void setName( String name ) {
   this.name = name;
 }

 public double getPrice( ) {
   return price;
 }

 public void setPrice( double price ) {
   this.price = price;
 }
 
 
}

FlowerCounter.java
Code
import java.text.NumberFormat;
import java.util.Scanner;

public class FlowerCounter {
 /*
  * Write a program named FlowerCounter that contains an array of 10 Flowers
  * objects and fill in it with the data given above accordingly. In your
  * program, the user should read the name of a flower and the quantity desired
  * by a customer. Locate the flower name in the array and use that index to
  * find the cost per stem. Compute and print each kind of the flower’s cost
  * and the total cost of the sale. In your program, a message should be
  * displayed if the user enters a flower name that doesn’t include on the list
  */

 public static void main( String[] args ) {
   Flower[] flowers = new Flower[10];

   /*
    * A flower stand has ten kinds of flowers – petunia, pansy, rose, violet,
    * carnation, lily, poppy, daisy, tulip, and hydrangea are stocked and cost,
    * respectively, 50¢, 75¢, $1.50, 75¢, 85¢, $1.25, $2.15, 85¢, $2.75, and
    * $1.30 per flower.
    */

   flowers[0] = new Flower( "petunia", 0.5 );
   flowers[1] = new Flower( "petupansynia", 0.75 );
   flowers[2] = new Flower( "rose", 1.5 );
   flowers[3] = new Flower( "violet", 0.75 );
   flowers[4] = new Flower( "carnation", 0.85 );
   flowers[5] = new Flower( "lily", 1.25 );
   flowers[6] = new Flower( "poppy", 2.15 );
   flowers[7] = new Flower( "daisy", 0.85 );
   flowers[8] = new Flower( "tulip", 2.75 );
   flowers[9] = new Flower( "hydrangea", 1.3 );

   for ( Flower flower : flowers ) {
     System.out.println( flower.getName( ) + " - "
         + NumberFormat.getCurrencyInstance( ).format( flower.getPrice( ) ) );
   }
   while ( true ) {
     boolean valid = false;
     System.out.println( "Enter name of the flower" );
     String name = new Scanner( System.in ).next( );
     double price = 0;
     for ( Flower flower : flowers ) {
       if ( flower.getName( ).equals( name ) ) {
         price = flower.getPrice( );
         valid = true;

       }
     }
     if ( valid ) {
       System.out.println( "Enter the quantity" );
       int count = new Scanner( System.in ).nextInt( );

       System.out.println( "The total cost is : "
           + NumberFormat.getCurrencyInstance( ).format( count * price ) );
     } else {
       System.out.println( "Flower (" + name + ") not found" );
     }
   }
 }
}


This post was edited by labatymo on Apr 17 2013 07:17am
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Apr 17 2013 07:31am
Complex.java

Code
/*
* 2. Complex class Create a class called Complex for performing arithmetic with
* complex numbers. Complex numbers have the form realPart + imaginaryPart * i
* (e.g., a + bi) where i is : 1 The class should contain: Two double data
* fields x and y that represent the real and imaginary part of a complex
* number.
*/
public class Complex {
 private double x;

 private double y;

 /* A no-arg constructor that creates a complex number (0 + 0i). */
 public Complex( ) {
   super( );
   this.x = 0;
   this.y = 0;
 }

 /*
  * A constructor that creates a Complex object with specified real and
  * imaginary part of the complex number.
  */
 public Complex( double x, double y ) {
   super( );
   this.x = x;
   this.y = y;
 }

 /* Two get methods for data fields x and y, respectively. */
 public double getX( ) {
   return x;
 }

 public double getY( ) {
   return y;
 }

 /*
  * A method named add that returns the addition of this complex number with
  * another. Complex numbers are added by adding the real and imaginary parts
  * of the summands. That is to say: (a+bi) + (c+di) = (a+c) + (b+d)i The
  * method is: public Complex add(Complex c) {}
  */
 public Complex add( Complex c ) {
   return new Complex( this.x + c.x, this.y + c.y );
 }

 /*
  * A method named subtract that returns the subtraction of this complex number
  * with another. Subtraction is defined by: (a+bi) + (c+di) = (a-c) + (b-d)i
  * The method is: public Complex subtract(Complex c) {}
  */
 public Complex subtract( Complex c ) {
   return new Complex( this.x - c.x, this.y - c.y );
 }

 /*
  * A method named multiply that returns the multiplication of this complex
  * with another. The multiplication of two complex numbers is defined by the
  * following formula: ( a+bi )( c+di ) = ( ac – bd ) , ( bc + ad )i The method
  * is: public Complex multiply(Complex c) {}
  */
 public Complex multiply( Complex c ) {

   return new Complex( (this.x * c.x - this.y * c.y), this.y * c.x
       + (this.x * c.y) );
 }

 /*
  * A toString() method that prints a Complex object in the form (a)+(b)i,
  * where a is the real part and b is the imaginary part.
  */
 public String toString( ) {
   return "(" + this.x + ")+(" + this.y + ")i";
 }

 public static void main( String[] args ) {
   Complex a = new Complex( 4, 5 );
   Complex b = new Complex( 3, 2 );
   System.out.println( a.add( b ).toString( ) );
   System.out.println( a.subtract( b ).toString( ) );
   System.out.println( a.multiply( b ).toString( ) );
 }
}


This post was edited by labatymo on Apr 17 2013 07:49am
Member
Posts: 4,635
Joined: Aug 17 2011
Gold: 27,309.41
Apr 17 2013 10:47am
/Thread

Got all the help needed :)
Go Back To Homework Help Topic List
Add Reply New Topic New Poll