75.8k views
1 vote
Java Programming-

Goals

The lab this lesson introduces students to Object Oriented Thinking. By the end of this lab, students should be able to

To apply class abstraction to develop software

To discover the relationships between classes

To design programs using the object-oriented paradigm

To use the String class to process immutable strings

Question- Sophie and Sally are learning about money. Every once in a while, their parents will give them penny or shilling coins (no pounds at their age!). They each keep their money in a special purse, and their parents may ask them how many pence or shillings coins they have. (Note we're not interested in the monetary values, just the number of coins. Thus someone might have 25 shilling coins or 20 penny coins.) The interaction between the girls' parents and Sophie and Sally is similar to the following:

Enter 1 for Sophie, 2 for Sally, or 0 to exit: 1
Enter 1 to give pence, 2 to give shillings, 3 to query her purse: 1
Enter the pence to give: 3

Enter 1 for Sophie, 2 for Sally, or 0 to exit: 2
Enter 1 to give pence, 2 to give shillings, 3 to query her purse: 2
Enter the shillings to give: 2

Enter 1 for Sophie, 2 for Sally, or 0 to exit: 2
Enter 1 to give pence, 2 to give shillings, 3 to query her purse: 2
Enter the shillings to give: 1

Enter 1 for Sophie, 2 for Sally, or 0 to exit: 1
Enter 1 to give pence, 2 to give shillings, 3 to query her purse: 3
The purse has 3 pence, 0 shillings

Enter 1 for Sophie, 2 for Sally, or 0 to exit: 2
Enter 1 to give pence, 2 to give shillings, 3 to query her purse: 3
The purse has 0 pence, 3 shillings

Enter 1 for Sophie, 2 for Sally, or 0 to exit: 0

To get started, consider the following questions:

What are the major nouns of the scenario? Sophie, Sally, parents, pence, shilling, coin, purse

What are the relationships between these nouns?

Sophie and Sally each have purses. Having a Daughter or Child object might be possible, but in this scenario the only thing interesting about Sophie and Sally is that they each have a purse. We can treat them as names of different purse objects (i.e., they are variables of type Purse).

the parents are really just a euphemism for the user of the program. We have to interact with the user, but don't need a specific class.

pence and shillings are coins, but they also just count something--the int data type will represent them just fine

a purse contains pence and shillings, and we also need to add pence and shillings to a purse, and fetch its number of pence and shilling coins. This looks like an object.

Write a program to implement this scenario. Use two classes. One class should contain a main method and manage the dialog with the user. The other class should represent a Purse. The design of the Purse class is up to you. Make sure that your instance variables are private, follow the naming conventions, etc.

Answer-

Purse.java


public class Purse {

private int pence;
private int shilling;

public void incPence(int n)
{
pence+=n;
}
public void incshilling(int n)
{
shilling+=n;
}

public int getPence()
{
return pence;
}
public int getshilling()
{
return shilling;
}

}

========================================================================

DemoDriver.java

import java.util.Scanner;

public class DemoDriver {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
Purse sophie=new Purse();
Purse sally=new Purse();

while(true)
{
System.out.println("Enter 1 for Sophie, 2 for Sally, or 0 to exit: \\");
int girl=sc.nextInt();
int ch;
int pence,shill;
if(girl==1)
{
System.out.println("Enter 1 to give pence, 2 to give shillings, 3 to query her purse:\\");
ch=sc.nextInt();

if(ch==1)
{
System.out.println("Enter the pence to give:\\");
pence=sc.nextInt();
sophie.incPence(pence);

}else if(ch==2)
{
System.out.println("Enter the shillings to give:\\");
shill=sc.nextInt();
sophie.incshilling(shill);
}
else
{
System.out.println("Purse has "+sophie.getPence()+" pence,"+sophie.getshilling()+" shillings\\");
}
}
else if(girl==2)
{
System.out.println("Enter 1 to give pence, 2 to give shillings, 3 to query her purse:\\");
ch=sc.nextInt();

if(ch==1)
{
System.out.println("Enter the pence to give:\\");
pence=sc.nextInt();
sally.incPence(pence);

}else if(ch==2)
{
System.out.println("Enter the shillings to give:\\");
shill=sc.nextInt();
sally.incshilling(shill);
}
else
{
System.out.println("Purse has "+sally.getPence()+" pence,"+sally.getshilling()+" shillings\\");
}

}else
{
break;
}

}
}

}

Please include the final results in the answer above as it is missing and also remove the break method from code. Also change the while loop to For loop

1 Answer

3 votes

Answer:

Java Programming

Details explained below.

Step-by-step explanation:

import java.util.Scanner;

public class DemoDriver {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner sc=new Scanner(System.in);

Purse sophie=new Purse();

Purse sally=new Purse();

while(true)

{

System.out.println("Enter 1 for Sophie, 2 for Sally, or 0 to exit: \\");

int girl=sc.nextInt();

int ch;

int pence,shill;

if(girl==1)

{

System.out.println("Enter 1 to give pence, 2 to give shillings, 3 to query her purse:\\");

ch=sc.nextInt();

if(ch==1)

{

System.out.println("Enter the pence to give:\\");

pence=sc.nextInt();

sophie.incPence(pence);

}else if(ch==2)

{

System.out.println("Enter the shillings to give:\\");

shill=sc.nextInt();

sophie.incshilling(shill);

}

else

{

System.out.println("Purse has "+sophie.getPence()+" pence,"+sophie.getshilling()+" shillings\\");

}

}

else if(girl==2)

{

System.out.println("Enter 1 to give pence, 2 to give shillings, 3 to query her purse:\\");

ch=sc.nextInt();

if(ch==1)

{

System.out.println("Enter the pence to give:\\");

pence=sc.nextInt();

sally.incPence(pence);

}else if(ch==2)

{

System.out.println("Enter the shillings to give:\\");

shill=sc.nextInt();

sally.incshilling(shill);

}

else

{

System.out.println("Purse has "+sally.getPence()+" pence,"+sally.getshilling()+" shillings\\");

}

}else

{

break;

}

}

}

} END.

User Opewix
by
5.1k points