0% found this document useful (0 votes)
20 views32 pages

10 Beans

The document contains multiple Java programs that perform various tasks, such as calculating exact change, checking for leap years, formatting names, decoding text message abbreviations, and validating user input. Each program utilizes the Scanner class for input and includes logic for processing that input to produce specific outputs. The programs demonstrate fundamental programming concepts such as loops, conditionals, and string manipulation.

Uploaded by

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

10 Beans

The document contains multiple Java programs that perform various tasks, such as calculating exact change, checking for leap years, formatting names, decoding text message abbreviations, and validating user input. Each program utilizes the Scanner class for input and includes logic for processing that input to produce specific outputs. The programs demonstrate fundamental programming concepts such as loops, conditionals, and string manipulation.

Uploaded by

Albert Zweistein
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

import java.util.

Scanner;

public class LabProgram {


public static void exactChange(int userTotal, int[] coinValue) {
coinValue[0] = userTotal / 100;
userTotal %= 100;
coinValue[1] = userTotal / 25;
userTotal %= 25;
coinValue[2] = userTotal / 10;
userTotal %= 10;
coinValue[3] = userTotal / 5;
userTotal %= 5;
coinValue[4] = userTotal;
}

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
int[] coins = new int[5];
int value = in.nextInt();
if(value <= 0) {
System.out.println("No change");
} else {
exactChange(value, coins);
if (coins[0] == 1) System.out.println(coins[0] + " Dollar");
else if (coins[0] >= 1) System.out.println(coins[0] + " Dollars");
if (coins[1] == 1) System.out.println(coins[1] + " Quarter");
else if (coins[1] > 1) System.out.println(coins[1] + " Quarters");
if (coins[2] == 1) System.out.println(coins[2] + " Dime");
if (coins[2] > 1) System.out.println(coins[2] + " Dimes");
if (coins[3] == 1) System.out.println(coins[3] + " Nickel");
if (coins[3] > 1) System.out.println(coins[3] + " Nickels");
if (coins[4] == 1) System.out.println(coins[4] + " Penny");
if (coins[4] > 1) System.out.println(coins[4] + " Pennies");
}
}
}
-------------------------------------------------------------------------------------------------------------
import java.util.Scanner;

public class LabProgram {


public static boolean leapYear(int year) {
if ( year % 4 == 0 && year % 100 != 0) {
return true;
}
else if (year % 400 == 0) {
return true;
}
return false;
}

public static void main(String[] args) {


int yearNum;
Scanner scnr = new Scanner(System.in);
yearNumber = scnr.nextInt();
if (leapYear(yearNum)) {
System.out.println(yearNum + " - leap year");
}
else {
System.out.println(yearNum + " - not a leap year");
}
}
}
-----------------------------------------------------------------------------------------------------------------
import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
String[] names = fullName.split("\\s+");

if (names.length == 2) {
System.out.println(names[1] + ", " + names[0].charAt(0) + ".");
} else if (names.length == 3) {
System.out.println(names[2] + ", " + names[0].charAt(0) + "." + names[1].charAt(0) + ".");
} else {
System.out.println("The input does not have correct form");
}

}
}
---------------------------------------------------------------------------------------------------
import java.util.Scanner;

public class TextMsgAbbreviation {


public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String input;

System.out.println("Input an abbreviation:");

input = scnr.nextLine();

if (input.contains("LOL")) {
System.out.println("laughing out loud");
}
else if (input.contains("BFF")) {
System.out.println("best friends forever");
}
else if (input.contains("IMHO")) {
System.out.println("in my humble opinion");
}
else if (input.contains("TMI")) {
System.out.println("too much information");
}
else if (input.contains("IDK")) {
System.out.println("I don't know");
}
else {
System.out.println("Unknown");
}
return;

}
}
-------------------------------------------------------------------------------------------
import java.util.Scanner;

public class TextMsgDecoder {

public static void main(String[] args) {

Scanner scnr = new Scanner(System.in);

System.out.println("Enter text:");

String input = scnr.nextLine();

if (!input.isEmpty()) {
System.out.println("You entered: " + input);

if (input.indexOf("BFF") != -1) {

System.out.println("BFF: best friend forever");

if (input.indexOf("IDK") != -1) {

System.out.println("IDK: I don't know");

if (input.indexOf("JK") != -1) {

System.out.println("JK: just kidding");

if (input.indexOf("TMI") != -1) {

System.out.println("TMI: too much information");

if (input.indexOf("TTYL") != -1) {

System.out.println("TTYL: talk to you later");

}
-------------------------------------------------------------------------------
import java.util.Scanner;
public class TextMsgExpander
{
public static void main(String[ ] args)
{
Scanner scan = new Scanner(System.in);
​ String text;

String BFF = "best friend forever";


String IDK = "I don't know";
String JK = "just kidding";
String TMI = "too much information";
String TTYL = "talk to you later";

System.out.println("Enter text:");
text=scan.nextLine();

System.out.println("You entered: "+text);


System.out.println();

if(text.contains("BFF"))
{
text=text.replace("BFF",BFF);
System.out.println("Replaced \"BFF\" with " + "\"" + BFF + "\"" + ".");
}

if(text.contains("IDK"))
{
text=text.replace("IDK",IDK);
System.out.println("Replaced \"IDK\" with " + "\"" + IDK + "\"" + ".");
}

if(text.contains("JK"))
{
text=text.replace("JK",JK);
System.out.println("Replaced \"JK\" with " + "\"" + JK + "\"" + ".");
}

if(text.contains("TMI"))
{
text=text.replace("TMI",TMI);
System.out.println("Replaced \"TMI\" with " + "\"" + TMI + "\"" + ".");
}

if(text.contains("TTYL"))
{
text=text.replace("TTYL",TTYL);
System.out.println("Replaced \"TTYL\" with " + "\"" + TTYL + "\"" + ".");
}
​ System.out.println();
​ System.out.println("Expanded: "+text);
​ }

​ }
-------------------------------------------------------------
import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
int input;

Scanner scan = new Scanner(System.in);

input = scan.nextInt();

while(input > 0){


System.out.print(input % 2);
input = input / 2;
}
System.out.print("\n");
}
}

---------------------------------------------------------------------
import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String word;
int count;
String word2;
int count2;

word = scan.next();
count = scan.nextInt();
word2 = scan.next();
count2 = scan.nextInt();

while (true) {

if (word.equals("quit") && count == 0)


break;
System.out.println("Eating " + count + " " + word + " a day keeps you happy and
healthy.");
if (word2.equals("quit") && count2 == 0)
break;
System.out.println("Eating " + count2 + " " + word2 + " a day keeps you happy and
healthy.");
break;
}
}
}
-------------------------------------------------------------
import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int count = 0
max = 0;
total = 0;

int num = scan.nextInt();


while (num >= 0) {
count++;
total += num;
max = Math.max(max, num);
num = scnr.nextInt();
}

int avg = count == 0 ? 0 : total/count;


System.out.println(max + " " + avg);
}
}
-----------------------------------------------------------------------------------------
import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
char userChar = scan.next().charAt(0);
String userString = scan.nextLine();

int result = 0;
for(int i = 0;i<userString.length();i++){
if(userString.charAt(i)==userChar){
result += 1;
}
}

if(result==1){
System.out.println(result+" "+userChar);
}
else{
System.out.println(result+" "+userChar+"'s");
}
}
}
----------------------------------------
import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userString;
boolean result = true;

userString = scnr.next();
for(char ch: userString.toCharArray()){
if(!Character.isDigit(ch)){
result = false;
}
}

if(result){
System.out.println("Yes");
}
else{
System.out.println("No");
}
}
}
------------------------------------------------------------
import java.util.Scanner;

public class LabProgram {

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
String password = in.nextLine();
String modified = "";
for(int i = 0; i < password.length(); ++i) {
if(password.charAt(i) == '1') {
modified += "!";
} else if(password.charAt(i) == 'a') {
modified += "@";
} else if(password.charAt(i) == 's') {
modified += "$";
} else if(password.charAt(i) == 'm') {
modified += "M";
} else if(password.charAt(i) == 'B') {
modified += "8";
} else if(password.charAt(i) == 'i') {
modified += "1";
} else {
modified += password.charAt(i);
}
}
modified += "!";
System.out.println(modified);
}

}
------------------------------------------------------------
import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String test= scnr.nextLine();

int len=test.length();

String alphaString="";

for(int i=0; i<len; i++){


if (Character.isLetter(test.charAt(i))) {
alphaString=alphaString+test.charAt(i);
}
}

System.out.println(alphaString);
}
}
-------------------------------------------------------
import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userText;
int count = 0;

userText = scnr.nextLine();

for (int i = 0; i < userText.length(); i++)


{
if(userText.charAt(i) != ' ' && userText.charAt(i) != '.' && userText.charAt(i) !=',' &&
userText.charAt(i) !='!' )
{
count++;
}
}
System.out.println(count);
}
}
---------------------------------------------------------------------
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int userDigit;
int newDigit;

userDigit = scnr.nextInt();

newDigit = userDigit - 1;

if ((userDigit > 10) && (userDigit < 101)) {


System.out.print(userDigit + " ");

}
else {
System.out.println("Input must be 11-100");
}
while ((userDigit > 10) && (userDigit < 101) && (newDigit % 11 != 0)) {
if (userDigit % 11 == 0) {
System.out.println();
break;
}
System.out.print(newDigit + " ");
--newDigit;

if (newDigit % 11 == 0) {
System.out.println(newDigit + " ");
break;
}
}

}
}
--------------------------------------------------------------------
import java.util.*;

public class LabProgram {


public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int firstInteger = scan.nextInt();
int secondInteger = scan.nextInt();
if (secondInteger >= firstInteger) {
while (firstInteger <= secondInteger) {
System.out.print(firstInteger + " ");
firstInteger += 5;
}
System.out.println();

} else {
System.out.println("Second integer can't be less than the first.");
}
}

}
-------------------------------------------------------------------
import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
while (!(s.equals("Done")||s.equals("done")||s.equals("d"))){
for(int i = s.length()-1;i>=0;i--){
System.out.print(s.charAt(i));
}
System.out.println();
s = scan.nextLine();
}
}
}
-------------------------------------------------------------------------------------------
import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s;

System.out.print("");
s = in.nextLine();

boolean palindrome = true;


int i = 0, j = s.length() - 1;
while(i < j){
if(s.charAt(i) == ' ')
i++;
else if(s.charAt(j) == ' ')
j--;
else if(s.charAt(i) != s.charAt(j)){
palindrome = false;
break;
}
else{
i++;
j--;
}
}

if(palindrome)
System.out.println("palindrome: "+ s);
else
System.out.println("not a palindrome: " + s);
}
}
-------------------------------------------------------------------------------------
import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int x1, y1, x2, y2, z1,z2;
x1 = scan.nextInt();
y1 = scan.nextInt();
z1 = scan.nextInt();

x2 = scan.nextInt();
y2 = scan.nextInt();
z2 = scan.nextInt();

boolean haveSolution = false;


int resX = 0, resY = 0;
for(int x=-10;x<=10;x++){
for(int y=-10;y<=10;y++){
if((x1*x+y1*y==z1) && (x2*x+y2*y==z2)){
resX = x;
resY = y;
haveSolution = true;
break;
}
}
if(haveSolution){
break;
}
}
if(haveSolution) {
System.out.println("x = " + resX + ", " + "y = "+resY);
}
else{
System.out.println("There is no solution");
}
}
}
----------------------------------------------------------
import java.util.Scanner;

public class DrawRightTriangle {

public static void main(String[] args) {


Scanner scnr = new Scanner(System.in);
char symbol;
int height;

System.out.println("Enter a character:");
symbol = scnr.next().charAt(0);

System.out.println("Enter triangle height:");


height = scnr.nextInt();

while (height < 1 || height > 10) {


System.out.println("Please enter height between 1 and 10.");
height = scnr.nextInt();
}

System.out.println("");

for (int i = 0; i < height; i++) {


for (int j = 0; j <= i; j++) {
System.out.print(symbol + " ");
}
System.out.println();
}
}
}
-----------------------------------------------------------------------------
import java.util.*;

public class DrawHalfArrow {


public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int arrowBaseHeight;
int arrowBaseWidth;
int arrowHeadWidth;

System.out.println("Enter arrow base height:");


arrowBaseHeight = scnr.nextInt();

System.out.println("Enter arrow base width:");


arrowBaseWidth = scnr.nextInt();

System.out.println("Enter arrow head width:");


arrowHeadWidth = scnr.nextInt();
if (arrowHeadWidth > arrowBaseWidth){
System.out.println("");
}

while (arrowHeadWidth <= arrowBaseWidth) {


System.out.println("Enter arrow head width:");
arrowHeadWidth = scnr.nextInt();
if (arrowHeadWidth > arrowBaseWidth) {
System.out.println("");
break;}
}

for (int i = 0; i < arrowBaseHeight; i++) {


for (int j = 0; j < arrowBaseWidth; j++) {
System.out.print("*");
} System.out.println();
}

for (int k = arrowHeadWidth; k <= arrowHeadWidth; k--) {


for(int l = 0; l < k; l++) {
System.out.print("*"); }
if (k == 0) {
break; }
System.out.println();
}
}
}
--------------------------------------------------------------------------------------------chapter 7
import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for(int i = 0; i < arr.length; ++i) {
arr[i] = in.nextInt();
}
for(int i = arr.length-1; i >= 0; --i) {
System.out.print(arr[i] + ",");
}
System.out.println();

}
}
------------------------------------------------------------------------------------------
import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int[] userValues = new int[9]; // Set of data specified by the user

int i = 0, n;

n = scnr.nextInt();

while (n != -1 && i < 9) {

userValues[i++] = n;

n = scnr.nextInt();
}

if(n==-1) {
System.out.println("Middle item: "+userValues[i / 2]);
}
else{
System.out.println("Too many numbers");
}
}
}
---------------------------------------------------------------

import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);

int numValues = scnr.nextInt();


int[] userValues = new int[numValues];
for (int i = 0; i < numValues; i++) {
userValues[i] = scnr.nextInt();
}
int threshold = scnr.nextInt();
for (int i = 0; i < numValues; ++i) {
if(userValues[i] <= threshold) {
System.out.print(userValues[i] + ",");
}
}
System.out.println();
}
}
------------------------------------------------------------
import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

int n;
n = scan.nextInt();
while(n<=0){
n = scan.nextInt();
}

float val[] = new float[n];


for(int i = 0;i<val.length;i++){
val[i] = scan.nextFloat();
}

float max = val[0];


for(int i = 1;i<val.length;i++){
if(max < val[i]){
max = val[i];
}
}

for(int i = 0;i<val.length;i++){
val[i] = val[i]/max;
}

for(int i = 0;i<val.length;i++){
System.out.printf("%.2f ",val[i]);
}
System.out.println();
}
}
-----------------------------------------------------------------
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String strArray[] = new String[n];

for (int i = 0; i < n; i++) {


strArray[i] = sc.next();
}
int countArray[] = new int[n];
int count;

for (int i = 0; i < countArray.length; i++) {


count = 0;
for (int j = 0; j < countArray.length; j++) {
if (strArray[i].equals(strArray[j])) {
count++;
}
}
countArray[i] = count;
}
for (int i = 0; i < strArray.length; i++) {
System.out.println(strArray[i] + " - " + countArray[i]);
}

}
}
--------------------------------------------------------------------
import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String[] words = new String[n];
for (int i = 0; i < words.length; i++) {
words[i] = in.next();
}
char ch = in.next().charAt(0);
for (int i = 0; i < words.length; i++) {
boolean found = false;
for (int j = 0; j < words[i].length(); j++) {
if (words[i].charAt(j) == ch) {
found = true;
}
}
if (found) {
System.out.print(words[i] + ",");
}
}
}
}
------------------------------------------------------------------------
import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
int min = in.nextInt();
int max = in.nextInt();
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= min && arr[i] <= max) {
System.out.print(arr[i] + ",");
}
}
System.out.println();
}
}
-------------------------------------------------------
import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int min1 = in.nextInt(), min2 = in.nextInt(), num;
if (min1 > min2) {
int temp = min1;
min1 = min2;
min2 = temp;
}
for (int i = 2; i < n; i++) {
num = in.nextInt();
if (num < min1) {
min2 = min1;
min1 = num;
}
else if (num < min2) {
min2 =num;
}
}
System.out.println(min1 + " and " + min2);
}
}
-------------------------------------------------------------------
import java.util.Scanner;

public class PeopleWeights {


public static void main(String[] args) {
/* Type your code here. */
Scanner scnr = new Scanner(System.in);
final int NUM_ELEMENTS = 5;
double[] userVals = new double[NUM_ELEMENTS];
int i;
int x;

for (i = 0; i < NUM_ELEMENTS; ++i) {


System.out.println("Enter weight " + (i + 1) + ":");
userVals[i] = scnr.nextDouble();
}

System.out.print("You entered: ");


for (i = 0; i < NUM_ELEMENTS; ++i) {
System.out.print(userVals[i] + " ");
}
System.out.println();
System.out.println("");
double totalWeight = 0.0;
for (i = 0; i < NUM_ELEMENTS; ++i) {
totalWeight += userVals[i];
}
System.out.println("Total weight: " + totalWeight);

double averageWeight = 0.0;


averageWeight = totalWeight / NUM_ELEMENTS;
System.out.println("Average weight: " + averageWeight);
double maxWeight = userVals[0];
for (i =0; i < NUM_ELEMENTS; ++i) {
if (userVals[i] > maxWeight) {
maxWeight = userVals[i];
}
}
System.out.println("Max weight: " + maxWeight);

return;
}
}
-------------------------------------------------------------------------
import java.util.Scanner;
public class PlayerRoster
{
/*printing the menu*/
public static void outputMenu()
{
System.out.println("MENU");
System.out.println("u - Update player rating");
System.out.println("a - Output players above a rating");
System.out.println("r - Replace player");
System.out.println("o - Output roster");
System.out.println("q - Quit\n");
System.out.println("Choose an option:");
}
/*printing the details of players*/
public static void outputRoster(int[] jersey, int rating[], int min)
{
System.out.println(((min>0) ? ("ABOVE " + min) : ("ROSTER")));

for (int i=0; i<5; i++)


{
if (rating[i] > min)
{
System.out.println("Player " + (i+1) + " -- Jersey number: " + jersey[i] + ", Rating: " +
rating[i]);
}
}
System.out.println();
}

public static void main(String[] args)


{
Scanner scnr = new Scanner(System.in);

/*declarations*/
int[] jersey = new int[5];
int[] rating = new int[5];
char input;
boolean keepAlive = true;

/*reading the jersey numbers and rating*/


for(int i=0; i<5; i++)
{
System.out.println("Enter player " + (i+1) + "'s jersey number:");
jersey[i] = scnr.nextInt();

System.out.println("Enter player " + (i+1) + "'s rating:");


rating[i] = scnr.nextInt();

System.out.println();
}

outputRoster(jersey, rating, 0); //calling the function to print details of players.

while(keepAlive)
{
outputMenu(); //calling the function to printing the menu options
input = scnr.next().charAt(0); //reading the user choice

if(input == 'q') //user want to quit.


{
keepAlive = false;
}
else if(input == 'o') //user want to print the details of players.
{
outputRoster(jersey, rating, 0); //calling the function to print details of players.
}
else if(input == 'u') //user want to update the player rating.
{
/*reading the jersey number*/
System.out.println("Enter a jersey number:");
int jerseyNum = scnr.nextInt();
/*reading the new rating for player*/
System.out.println("Enter a new rating for player:");
int newRating = scnr.nextInt();
/*updating the rating of player*/
for(int i=0; i<5; i++)
{
if(jersey[i] == jerseyNum)
{
rating[i] = newRating;
}
}
System.out.println();
}
else if(input == 'a') //user want to print details above rating.
{
/*reading the rating*/
System.out.println("Enter a rating:");
int above_rat = scnr.nextInt();
outputRoster(jersey, rating, above_rat); //calling the function to print details of players
above rating.
}
else if(input == 'r') //user want to replace the player.
{
/*reading the jersey number of plyer to replace*/
System.out.println("Enter a jersey number:");
int jerseyNum = scnr.nextInt();

/*replacing the player*/


for(int i=0; i<5; i++)
{
if(jersey[i] == jerseyNum)
{
System.out.println("Enter a new jersey number:");
jersey[i] = scnr.nextInt();

System.out.println("Enter a rating for the new player:");


rating[i] = scnr.nextInt();
}
}
System.out.println();
}
}
}
}
-----------------------------------------------------------------chapter 8
import java.util.Scanner;
public class LabProgram {

public static double stepsToMiles(int userSteps) {


return userSteps / 2000.0;
}

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);
System.out.printf("%.2f", stepsToMiles(in.nextInt()));
}
}
--------------------------------------------------------------------
import java.util.Scanner;

public class ShoppingCartPrinter {

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);
ItemToPurchase item1 = new ItemToPurchase();
ItemToPurchase item2 = new ItemToPurchase();
System.out.println("Item 1");
System.out.println("Enter the item name:");
String name1 = scan.nextLine();
System.out.println("Enter the item price:");
int price1 = scan.nextInt();
System.out.println("Enter the item quantity:");
System.out.println();
int quantity1 = scan.nextInt();
item1.setName(name1);
item1.setPrice(price1);
item1.setQuantity(quantity1);
scan.nextLine();
System.out.println("Item 2");
System.out.println("Enter the item name:");
String name2 = scan.nextLine();
System.out.println("Enter the item price:");
int price2 = scan.nextInt();
System.out.println("Enter the item quantity:");
System.out.println();
int quantity2 = scan.nextInt();
item2.setName(name2);
item2.setPrice(price2);
item2.setQuantity(quantity2);
System.out.println("TOTAL COST");
int item1Total = item1.getPrice() * item1.getQuantity();
int item2Total = item2.getPrice() * item2.getQuantity();
System.out.println(item1.getName()+" "+item1.getQuantity()+" @ $"+item1.getPrice()+" =
$"+item1Total);
System.out.println(item2.getName()+" "+item2.getQuantity()+" @ $"+item2.getPrice()+" =
$"+item2Total);
System.out.println();
System.out.println("Total: $"+(item1Total + item2Total));

}
---------------------------------------------------chaoter 9
import java.util.Scanner;
import java.lang.Math;

public class CarValue {


public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);

Car myCar = new Car();

int userYear = scnr.nextInt();


int userPrice = scnr.nextInt();
int userCurrentYear = scnr.nextInt();

myCar.setModelYear(userYear);
myCar.setPurchasePrice(userPrice);
myCar.calcCurrentValue(userCurrentYear);

myCar.printInfo();
}
}
---------------------------------
public class FoodItem {

private String name;


private double fat;
private double carbs;
private double protein;

//Default Constructor
public FoodItem(){
name="None";
fat = carbs = protein =0;
}

//Parameterized constructor
public FoodItem(String name, double fat, double carbs, double protein) {
this.name = name;
this.fat = fat;
this.carbs = carbs;
this.protein = protein;
}

public String getName() {


return name;
}
public double getFat() {
return fat;
}
public double getCarbs() {
return carbs;
}
public double getProtein() {
return protein;
}

public double getCalories(double numServings) {


//calorie formula
double calories = ((fat * 9) + (carbs*4) + (protein * 4)) * numServings;
return calories;
}

public void printInfo() {


System.out.println("Nutritional information per serving of " + name +": ");
System.out.printf(" Fat: %.2f g\n", fat);
System.out.printf(" Carbohydrates: %.2f g\n", carbs);
System.out.printf(" Protein: %.2f g\n", protein);
}
}
--------------------------------------------------------------------------------------9.24
import java.util.Scanner;

public class ArtworkLabel {


public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);

String userTitle, userArtistName;


int yearCreated, userBirthYear, userDeathYear;

userArtistName = scnr.nextLine();
userBirthYear = scnr.nextInt();
scnr.nextLine();
userDeathYear = scnr.nextInt();
scnr.nextLine();
userTitle = scnr.nextLine();
yearCreated = scnr.nextInt();

Artist userArtist = new Artist(userArtistName, userBirthYear, userDeathYear);

Artwork newArtwork = new Artwork(userTitle, yearCreated, userArtist);

newArtwork.printInfo();
}
}
--------------------------------------------------------------chapter 10
public class Dog extends Pet {
private String breed;

public void setBreed(String userBreed) {


breed = userBreed;
}

public String getBreed() {


return breed;
}
}
-------------------------------------------------------------

import java.util.Scanner;

public class InstrumentInformation {


public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);

Instrument myInstrument = new Instrument();


StringInstrument myStringInstrument = new StringInstrument();

String instrumentName, manufacturerName, stringInstrumentName, stringManufacturer;


int yearBuilt, cost, stringYearBuilt, stringCost, numStrings, numFrets;

instrumentName = scnr.nextLine();
manufacturerName = scnr.nextLine();
yearBuilt = scnr.nextInt();
scnr.nextLine();
cost = scnr.nextInt();
scnr.nextLine();
stringInstrumentName = scnr.nextLine();
stringManufacturer = scnr.nextLine();
stringYearBuilt = scnr.nextInt();
stringCost = scnr.nextInt();
numStrings = scnr.nextInt();
numFrets = scnr.nextInt();

myInstrument.setName(instrumentName);
myInstrument.setManufacturer(manufacturerName);
myInstrument.setYearBuilt(yearBuilt);
myInstrument.setCost(cost);
myInstrument.printInfo();

myStringInstrument.setName(stringInstrumentName);
myStringInstrument.setManufacturer(stringManufacturer);
myStringInstrument.setYearBuilt(stringYearBuilt);
myStringInstrument.setCost(stringCost);
myStringInstrument.setNumOfStrings(numStrings);
myStringInstrument.setNumOfFrets(numFrets);
myStringInstrument.printInfo();

System.out.println(" Number of strings: " + myStringInstrument.getNumOfStrings());


System.out.println(" Number of frets: " + myStringInstrument.getNumOfFrets());
}
}
-----------------------------------------------------------

import java.util.Scanner;

public class CourseInformation {


public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);

Course myCourse = new Course();


OfferedCourse myOfferedCourse = new OfferedCourse();
String courseNumber, courseTitle;
String oCourseNumber, oCourseTitle, instructorName, term, classTime;

courseNumber = scnr.nextLine();
courseTitle = scnr.nextLine();

oCourseNumber = scnr.nextLine();
oCourseTitle = scnr.nextLine();
instructorName = scnr.nextLine();
term = scnr.nextLine();
classTime = scnr.nextLine();

myCourse.setCourseNumber(courseNumber);
myCourse.setCourseTitle(courseTitle);
myCourse.printInfo();

myOfferedCourse.setCourseNumber(oCourseNumber);
myOfferedCourse.setCourseTitle(oCourseTitle);
myOfferedCourse.setInstructorName(instructorName);
myOfferedCourse.setTerm(term);
myOfferedCourse.setClassTime(classTime);
myOfferedCourse.printInfo();

System.out.println(" Instructor Name: " + myOfferedCourse.getInstructorName());


System.out.println(" Term: " + myOfferedCourse.getTerm());
System.out.println(" Class Time: " + myOfferedCourse.getClassTime());
}
}
--------------------------------------------

import java.util.Scanner;

public class BookInformation {


public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);

Book myBook = new Book();


Encyclopedia myEncyclopedia = new Encyclopedia();

String title, author, publisher, publicationDate;


String eTitle, eAuthor, ePublisher, ePublicationDate, edition;
int numVolumes;

title = scnr.nextLine();
author = scnr.nextLine();
publisher = scnr.nextLine();
publicationDate = scnr.nextLine();

eTitle = scnr.nextLine();
eAuthor = scnr.nextLine();
ePublisher = scnr.nextLine();
ePublicationDate = scnr.nextLine();
edition = scnr.nextLine();
numVolumes = scnr.nextInt();

myBook.setTitle(title);
myBook.setAuthor(author);
myBook.setPublisher(publisher);
myBook.setPublicationDate(publicationDate);
myBook.printInfo();

myEncyclopedia.setTitle(eTitle);
myEncyclopedia.setAuthor(eAuthor);
myEncyclopedia.setPublisher(ePublisher);
myEncyclopedia.setPublicationDate(ePublicationDate);
myEncyclopedia.setEdition(edition);
myEncyclopedia.setNumVolumes(numVolumes);
myEncyclopedia.printInfo();

}
}
-------------------------------------------------------------------
import java.util.Scanner;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class PlantArrayListExample {

public static void printArrayList(ArrayList<Plant>myGarden) {

int i;

for (i = 0; i < myGarden.size(); ++i) {

myGarden.get(i).printInfo();
System.out.println();

}
}

public static void main(String[] args) {

Scanner scnr = new Scanner(System.in);


String input;

ArrayList<Plant> myGarden = new ArrayList<>();

String plantName;
String plantCost;

String colorOfFlowers;
boolean isAnnual;

input = scnr.next();

while(!input.equals("-1")) {

plantName = scnr.next();
plantCost = scnr.next();

if (input.equals("plant")) {

Plant myPlant = new Plant();

myPlant.setPlantName(plantName);
myPlant.setPlantCost(plantCost);

myGarden.add(myPlant);

}
else if (input.equals("flower")) {

Flower myFlower = new Flower();

myFlower.setPlantName(plantName);
myFlower.setPlantCost(plantCost);

isAnnual = scnr.nextBoolean();
colorOfFlowers = scnr.next();

myFlower.setPlantType(isAnnual);
myFlower.setColorOfFlowers(colorOfFlowers);

myGarden.add(myFlower);

input = scnr.next();

printArrayList(myGarden);

}
--------------------------------------------------------------------

You might also like