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

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 17:40, lazerlemon500
Write a modular program (no classes yet, just from what you learned last year), that allows two players to play a game of tic-tac-toe. use a two-dimensional char array with 3 rows and 3 columns as the game board. each element of the array should be initialized with an asterisk (*). the program should display the initial board configuration and then start a loop that does the following: allow player 1 to select a location on the board for an x by entering a row and column number. then redisplay the board with an x replacing the * in the chosen location. if there is no winner yet and the board is not yet full, allow player 2 to select a location on the board for an o by entering a row and column number. then redisplay the board with an o replacing the * in the chosen location. the loop should continue until a player has won or a tie has occurred, then display a message indicating who won, or reporting that a tie occurred. player 1 wins when there are three xs in a row, a column, or a diagonal on the game board. player 2 wins when there are three ox in a row, a column, or a diagonal on the game board. a tie occurs when all of the locations on the board are full, but there is no winner. input validation: only allow legal moves to be entered. the row must be 1, 2, or 3. the column must be 1, 2 3. the (row, column) position entered must currently be empty (i. e., still have an asterisk in it).
Answers: 1
image
Computers and Technology, 22.06.2019 20:00, ksanchez2100
Need asap assignment directions: think of an organization (business, religious institution, volunteer organization, sports team) with which you have been involved. imagine outfitting it with an it infrastructure. prepare a plan for what you would do to support outfitting it. draw a map of a network connecting all the individuals, give them pcs and printers, and lay out the design as best you can. the purpose is to begin working with these concepts, not to build a perfect network.
Answers: 2
image
Computers and Technology, 23.06.2019 06:00, hilarydodard7099
Which statistical function in a spreadsheet you to see how far each number varies, on average, from the average value of the list?
Answers: 2
image
Computers and Technology, 23.06.2019 13:30, Gearyjames8
Anetwork security application that prevents access between a private and trusted network and other untrusted networks
Answers: 1
You know the right answer?
Java Programming-

Goals

The lab this lesson introduces students to Object Or...

Questions in other subjects:

Konu
Arts, 23.02.2021 01:00