0% found this document useful (0 votes)
15 views5 pages

Java Code for Banana Purchase Logic

Uploaded by

HillBIlly
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Java Code for Banana Purchase Logic

Uploaded by

HillBIlly
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

if (numBananas<3){

[Link]("numBananas is less than the required amount to


purchase.");
}
else if(numBananas>=3){
int totalCost=numBananas*2;
if (totalCost<=dollarAmount){
[Link]("Approved transaction.");
}
if (dollarAmount>=2){
[Link]("At least one item was purchased.");
}
else {
[Link]("Not enough money to buy all.");
}
}
if (numBananas>23){
[Link]("Restocking soon.");
}
else{
[Link]("Item still in stock.");
}

the string fullName has three names, each separated with a white space aka empty
space ' '. output the last name in the string.
start output at substring
or
start at fullName substring at

group1=(yellow+brown)
last of group1 should be = brown
=> lastOfGroup1=[Link]('+')+1;
also needs to int something else to cap the input before the parenthesis
so => int lastOfGroup1End=[Link]()-1;

group2=(green+teal)
last of group2 should be = teal
=> lastOfGroup1=[Link]('+')+1;
also needs to int something else to cap the input before the parenthesis
so => int lastOfGroup2End=[Link]()-1;

group3=(indigo+magenta)
last of group1 should be = magenta
=> lastOfGroup1=[Link]('+')+1;
also needs to int something else to cap the input before the parenthesis
so => int lastOfGroup3End=[Link]()-1;
MY CODE:

int numCents = [Link]();

int numDollars = (numCents/100) % 100;


int numQuarters = (numCents % 100) / 25;
int numDimes = (numCents % 25) / 10;
int numNickles = (((numCents % 25) / 10) / 5);
int numPennies = (((numCents % 25) / 10) % 5);

if (Numcents == 0){
[Link]("No change");
}
if (numDollars < 2 && numDollars == 1){
[Link](numDollars + "Dollar");
}
else if (numDollars >= 2){
[Link](numDollars + "Dollars");
}

if (numQuarters < 2 && numQuarters == 1){


[Link](numQuarters + "Quarter");
}
else if(numQuarters >= 2){
[Link](numQuarters + "Quarters");
}

if (numDimes < 2 && numDimes == 1) {


[Link](numDimes + " Dime");
}
else if (numDimes >= 2) {
[Link](numDimes + " Dimes");
}

if (numNickels < 2 && numNickels == 1) {


[Link](numNickels + " Nickel");
}
else if (numNickels >= 2) {
[Link](numNickels + " Nickels");
}

if (numPennies < 2 && numPennies == 1) {


[Link](numPennies + " Penny");
}
else if (numPennies >= 2) {
[Link](numPennies + " Pennies");
}

CORRECT CODE:
Scanner scnr = new Scanner([Link]);

int cents = [Link]();

int numDollars = (cents / 100) % 100;


int numQuarters = (cents % 100) / 25;
int numDimes = (cents % 25) / 10;
int numNickels = (((cents % 25) % 10) / 5);
int numPennies = (((cents % 25) % 10) % 5);

if (cents == 0) {
[Link]("No change");
}

if (numDollars < 2 && numDollars == 1) {


[Link](numDollars + " Dollar");
}

else if (numDollars >= 2) {


[Link](numDollars + " Dollars");
}

if (numQuarters < 2 && numQuarters == 1) {


[Link](numQuarters + " Quarter");
}

else if (numQuarters >= 2) {


[Link](numQuarters + " Quarters");
}

if (numDimes < 2 && numDimes == 1) {


[Link](numDimes + " Dime");
}

else if (numDimes >= 2) {


[Link](numDimes + " Dimes");
}

if (numNickels < 2 && numNickels == 1) {


[Link](numNickels + " Nickel");
}

else if (numNickels >= 2) {


[Link](numNickels + " Nickels");
}

if (numPennies < 2 && numPennies == 1) {


[Link](numPennies + " Penny");
}

else if (numPennies >= 2) {


[Link](numPennies + " Pennies");
}
Many documents use a specific format for a person's name. Write a program whose
input is:

firstName middleName lastName

and whose output is:

lastName, [Link].

So i need to reassemble the first name last name and middle name
i need the full last name but only the first initials of the first and middle name,
in this order,
with a comma and space after the last name; periods between the first and middle
initial
as well as a period after the middle initial (last name, first [Link]
initial.)

If there isn't a middle name, therefore no middle initial, the ouput format should
be
last name, comma space, then first initial plus a period.

finding last name should use substring on String fullName

String fullName = [Link](); gets the full name (first , middle , last)

now break it up with indexof

mycode;

Scanner scnr = new Scanner([Link]);


String fullName = [Link]();

//find last name


int index1 = [Link](' ')+1;
int index2 = [Link]();
String lastName = [Link](index1, index2);

//find middle initial


int index3 = [Link](' ')+1;
int index4 = [Link](' ')+2;
String middleI = [Link](index3, index4);

//find first inital


String index5 = [Link](0, 1);
String firstI = index5;

//reassemble with if-else statement


[Link](lastName + ", " + firstI + "." + middleI + ".");
if (((inputYear%4==0)&&(inputYear%100!=0)||(inputYear%400==0))){
[Link](inputYear + " is a leap year");
}
else {
[Link](inputYear + " is not a leap year");
}

inputYear = [Link]();
if (inputYear % 4 ==0 && inputYear% inputYear % 100 != 0){
isLeapYear = true;
}
else if(inputYear % 400 == 0){
isLeapYear = true;
}
else{
isLeapYear = false;
}
if (isLeapYear=true){
[Link](inputYear + " is a leap year.");
}
else {
[Link](inputYear + " is not a leap year.");
}

You might also like