subject

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: \n");
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:\n");
ch=sc. nextInt();

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

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

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

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

}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

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 05:30
Sally is editing her science report about living things. she needs to copy a paragraph from her original report. order the steps sally needs to do to copy the text to her new document.
Answers: 1
question
Computers and Technology, 23.06.2019 19:40
Use a physical stopwatch to record the length of time it takes to run the program. calculate the difference obtained by calls to the method system.currenttimemillis() just before the start of the algorithm and just after the end of the algorithm. calculate the difference obtained by calls to the method system.currenttimemillis() at the start of the program and at the end of the program so that the elapsed time includes the display of the result. use the value returned by the method system.currenttimemillis() just after the end of the algorithm as the elapsed time.
Answers: 3
question
Computers and Technology, 23.06.2019 23:30
Match the following errors with their definitions. a. #name b. #value c. #ref d. 1. when a formula produces output that is too lengthy to fit in the spreadsheet cell 2. when you enter an invalid cell reference in a formula 3. when you type text in cells that accept numeric data 4. when you type in a cell reference that doesn’t exist
Answers: 1
question
Computers and Technology, 24.06.2019 03:30
Auniform resource locator (url) is a formatted string of text that web browsers, email applications, and other software programs use to identify a particular resource on the internet. true false
Answers: 2
You know the right answer?
Java Programming-

Goals

The lab this lesson introduces students to Object Or...
Questions
question
Social Studies, 04.05.2021 23:20
question
Mathematics, 04.05.2021 23:20
question
Mathematics, 04.05.2021 23:20
question
Mathematics, 04.05.2021 23:20