This commit is contained in:
Logen Kain 2020-11-11 08:47:37 -05:00
commit e168d9d8db
22 changed files with 712 additions and 0 deletions

View File

@ -0,0 +1,13 @@
/*
* Following Specs:
* Init in default constructor:
*
* String itemName -- init to none
* int itemPrice - init to 0
* int itemQuanity - init to 0
*
* Public member mothods (mutators & accessors)
* setName() & getName()
* setPrice() & getPrice()
* setQuantity() & getQuantity()
*/

View File

@ -0,0 +1,41 @@
/*
In main, ask the user for two items and create two objects of the
ItemToPurchase class.
Before asking for the second item, call scnr.nextLine()
to allow the user to put in a new string.
Example:
Item 1
Enter the item name:
Chocolate Chips
Enter the item price:
3
Enter the item quantity:
1
Item 2
Enter the item name:
Bottled Water
Enter the item price:
1
Enter the item quantity:
10
Then add the costs of teh two items together and output
the total cost
Example:
TOTAL COST
Chocolate Chips 1 @ $3 = $3
Bottled Water 10 @ $1 = $10
Total: $13
*/
import java.util.Scanner;
public class ShoppingCartPrinter {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
}
}

36
brute-force/main.java Normal file
View File

@ -0,0 +1,36 @@
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
/* Type your code here. */
Scanner scnr = new Scanner(System.in);
int eq1Co1, eq1Co2, eq1Sol, eq2Co1, eq2Co2, eq2Sol;
int xSol, ySol;
boolean sol = false;
eq1Co1 = scnr.nextInt();
eq1Co2 = scnr.nextInt();
eq1Sol = scnr.nextInt();
eq2Co1 = scnr.nextInt();
eq2Co2 = scnr.nextInt();
eq2Sol = scnr.nextInt();
int x, y;
for(x= -10; x<10; x++){
if (sol==true){break;}
for(y= -10; y<10; y++){
if ( ((x*eq1Co1) + (y*eq1Co2) == eq1Sol) && ((x*eq2Co1) + (y*eq2Co2) == eq2Sol)){
xSol = x;
ySol = y;
System.out.println(""+xSol+" "+ySol);
sol=true;
}
}
}
if (sol==false){
System.out.println("No solution");
}
}
}

14
brute-force/test.java Normal file
View File

@ -0,0 +1,14 @@
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
/* Type your code here. */
Scanner scnr = new Scanner(System.in);
int x;
int z = 0;
x = scnr.nextInt();
if (x > 9)
z = 3;
z = z + 1;
System.out.print(z);
}}

View File

@ -0,0 +1,50 @@
/* Joseph Green
* 7-Oct-2020
*
* take int for number of words
* take list of words
* take a char
*
* print out every word in the list that contains the char at least once
* Assume at least one word will contain the given char
* words will be < 20
*/
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
/* Type your code here. */
int numWords;
char someChar;
Scanner scnr = new Scanner(System.in);
String[] words = new String[20];
numWords = scnr.nextInt();
for (int i = 0; i<numWords; i++){
words[i] = scnr.next();
}
someChar = scnr.next().charAt(0);
for (int j = 0; j<numWords;j++){
for (int i = 0; i<words[j].length();i++){
if (someChar == words[j].charAt(i)){
System.out.println(words[j]);
break;
}
}
}
/* for (String word : words){
for (int i = 0; i<word.length();i++){
if (someChar == word.charAt(i)){
System.out.println(word);
break;
}
}
}*/
}
}

View File

@ -0,0 +1,33 @@
/* Joseph Green
* 7-Oct-2020
*
* take int for number of words
* take list of words
* take a char
*
* print out every word in the list that contains the char at least once
* Assume at least one word will contain the given char
* words will be < 20
*/
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
/* Type your code here. */
String[] words = {"jack", "jill", "james"};
for (String word : words){
System.out.println(word.length());
}
String newWord = "jack";
for (char ch : newWord){
System.out.println(ch);
}
}
}

View File

@ -0,0 +1,44 @@
/*
* Joseph Green
* 07-Oct-2020
*
* list of ints from input
*
* first int describes how many ints follow
*
* assume less than 20
*
* two more ints that describe lower and upper bounds of a range
*
* inclusevely print all numbers in the list from that range
*
* each output followed by space, even the last
*/
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
/* Type your code here. */
Scanner scnr = new Scanner(System.in);
int totalInts;
int max;
int min;
int[] ints = new int[20];
totalInts = scnr.nextInt();
for (int i =0; i<totalInts;i++){
ints[i] = scnr.nextInt();
}
min = scnr.nextInt();
max = scnr.nextInt();
for (int i =0; i<totalInts;i++){
if (ints[i] >= min && ints[i] <= max){
System.out.print(ints[i] + " ");
}
}
System.out.println();
}
}

View File

@ -0,0 +1,52 @@
public class FoodItem {
private String name;
private double fat;
private double carbs;
private double protein;
// TODO: Define default constructor
public FoodItem() {
name = "None";
fat = 0.0;
carbs = 0.0;
protein = 0.0;
}
// TODO: Define second constructor with arguments to initialize
// private fields (name, fat, carbs, protein)
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);
}
}

View File

@ -0,0 +1,28 @@
import java.util.Scanner;
public class NutritionalInfo {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
FoodItem foodItem1 = new FoodItem();
String itemName = scnr.next();
double amountFat = scnr.nextDouble();
double amountCarbs = scnr.nextDouble();
double amountProtein = scnr.nextDouble();
FoodItem foodItem2 = new FoodItem(itemName, amountFat, amountCarbs, amountProtein);
double numServings = scnr.nextDouble();
foodItem1.printInfo();
System.out.printf("Number of calories for %.2f serving(s): %.2f\n", numServings,
foodItem1.getCalories(numServings));
System.out.println("\n");
foodItem2.printInfo();
System.out.printf("Number of calories for %.2f serving(s): %.2f\n", numServings,
foodItem2.getCalories(numServings));
}
}

View File

@ -0,0 +1,29 @@
import java.util.Scanner;
public class LabProgram {
/* Define your method here */
public static int largestNumber(int num1, int num2, int num3){
int largest = num1;
largest = (largest < num2) ? num2:largest;
largest = (largest < num3) ? num3:largest;
return largest;
}
public static int smallestNumber(int num1, int num2, int num3){
int smallest = num1;
smallest = (smallest > num2) ? num2:smallest;
smallest = (smallest > num3) ? num3:smallest;
return smallest;
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int num1, num2, num3;
num1 = scnr.nextInt();
num2 = scnr.nextInt();
num3 = scnr.nextInt();
System.out.println("largest: " + largestNumber(num1,num2,num3));
System.out.println("smallest: " + smallestNumber(num1,num2,num3));
}
}

View File

@ -0,0 +1,41 @@
import java.util.Scanner;
public class LabProgram {
public static boolean isArrayEven(int[] arrayValues, int arraySize){
for (int i = 0; i<arraySize;i++){
if (arrayValues[i]%2 != 0){
return false;
}
}
return true;
}
public static boolean isArrayOdd(int[] arrayValues, int arraySize){
for (int i = 0; i<arraySize;i++){
if (arrayValues[i]%2 == 0){
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int[] myInts = new int[20];
int numOfInts = scnr.nextInt();
for (int i = 0; i<numOfInts;i++){
myInts[i] = scnr.nextInt();
}
if (isArrayEven(myInts, numOfInts)){
System.out.println("all even");
}
else if (isArrayOdd(myInts, numOfInts)){
System.out.println("all odd");
}
else{
System.out.println("not even or odd");
}
}
}

View File

@ -0,0 +1,40 @@
import java.util.Scanner;
public class LabProgram {
/* Define your method here */
public static void sortArray(int[] myArr, int arrSize){
int temp;
int flag;
for(int i = 0; i < arrSize-1;i++){
flag = 0;
for(int j = 0; j < arrSize-1; j++){
if (myArr[j] > myArr[j+1]){
temp = myArr[j];
myArr[j] = myArr[j+1];
myArr[j+1] = temp;
flag = 1;
}
}
if (flag == 0){
break;
}
}
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int[] myInts = new int[20];
int myIntsLength = scnr.nextInt();
for (int i=0;i < myIntsLength;i++){
myInts[i] = scnr.nextInt();
}
sortArray(myInts, myIntsLength);
for(int i=0; i<myIntsLength; i++){
System.out.print(myInts[i] + " ");
}
System.out.println();
}
}

View File

@ -0,0 +1,43 @@
import java.util.Scanner;
public class LabProgram {
public static int getFrequencyOfWord(String[] wordsList,
int listSize, String currWord){
int frequency = 0;
for (int i=0; i<listSize;i++){
if (wordsList[i].equals(currWord)){
frequency +=1;
}
}
return frequency;
}
public static void main(String[] args) {
/* Type your code here. */
Scanner scnr = new Scanner(System.in);
int numOfWords;
int i;
String[] listOfWords = new String[20];
int[] frequency = new int[20];
numOfWords = scnr.nextInt();
for (i=0; i<numOfWords;i++){
listOfWords[i] = scnr.next();
}
for (i=0; i<numOfWords;i++){
frequency[i] = getFrequencyOfWord(listOfWords, numOfWords, listOfWords[i]);
}
for (i=0; i<numOfWords;i++){
System.out.println(listOfWords[i] + " " + frequency[i]);
}
}
}

View File

@ -0,0 +1,47 @@
public class Car {
private int modelYear;
// TODO: Declare purchasePrice field (int)
private int purchasePrice;
private int currentValue;
public void setModelYear(int userYear){
modelYear = userYear;
}
public int getModelYear() {
return modelYear;
}
// TODO: Define setPurchasePrice() method
public void setPurchasePrice(int userPrice) {
purchasePrice = userPrice;
}
// TODO: Define getPurchasePrice() method
public int getPurchasePrice() {
return purchasePrice;
}
public int getCurrentValue() {
return currentValue;
}
public void calcCurrentValue(int currentYear) {
double depreciationRate = 0.15;
int carAge = currentYear - modelYear;
// Car depreciation formula
currentValue = (int)
Math.round(purchasePrice * Math.pow((1 - depreciationRate), carAge));
}
// TODO: Define printInfo() method to output modelYear, purchasePrice, and currentValue
public void printInfo() {
System.out.println("Car's information:");
System.out.println(" Model year: " + getModelYear()); // Just to show I can do it
System.out.println(" Purchase price: " + purchasePrice); //No idea why it bothered with getPurchasePrice()
System.out.println(" Current value: " + currentValue);
}
}

View File

@ -0,0 +1,21 @@
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();
}
}

View File

@ -0,0 +1,40 @@
public class Team {
// TODO: Declare private fields - teamName, teamWins, teamLosses
private String teamName;
private int teamWins;
private int teamLosses;
// TODO: Define mutator methods -
// setTeamName(), setTeamWins(), setTeamLosses()
public void setTeamName(String name){
teamName=name;
}
public void setTeamWins(int wins){
teamWins = wins;
}
public void setTeamLosses(int lose){
teamLosses = lose;
}
// TODO: Define accessor methods -
// getTeamName(), getTeamWins(), getTeamLosses()
public String getTeamName(){
return teamName;
}
public int getTeamLosses(){
return teamLosses;
}
public int getTeamWins(){
return teamWins;
}
// TODO: Define getWinPercentage()
public double getWinPercentage(){
return (double)teamWins / (teamWins + teamLosses); //Java is not smart about doubles double avg = 1/2 results in int div
}
}

View File

@ -0,0 +1,27 @@
import java.util.Scanner;
public class WinningTeam {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
Team team = new Team();
String name = scnr.next();
int wins = scnr.nextInt();
int losses = scnr.nextInt();
team.setTeamName(name);
team.setTeamWins(wins);
team.setTeamLosses(losses);
if (team.getWinPercentage() >= 0.5) {
System.out.println("Congratulations, Team " + team.getTeamName() +
" has a winning average!");
}
else {
System.out.println("Team " + team.getTeamName() +
" has a losing average.");
}
}
}

Binary file not shown.

View File

@ -0,0 +1,22 @@
public class Triangle {
private double base;
private double height;
public void setBase(double userBase){
base = userBase;
}
public void setHeight(double userHeight) {
height = userHeight;
}
public double getArea() {
double area = 0.5 * base * height;
return area;
}
public void printInfo() {
System.out.printf("Base: %.2f\n", base);
System.out.printf("Height: %.2f\n", height);
System.out.printf("Area: %.2f\n", getArea());
}
}

View File

@ -0,0 +1,28 @@
import java.util.Scanner;
public class TriangleArea {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
Triangle triangle1 = new Triangle();
Triangle triangle2 = new Triangle();
// TODO: Read and set base and height for triangle1 (use setBase() and setHeight())
triangle1.setBase(scnr.nextDouble());
triangle1.setHeight(scnr.nextDouble());
// TODO: Read and set base and height for triangle2 (use setBase() and setHeight())
triangle2.setBase(scnr.nextDouble());
triangle2.setHeight(scnr.nextDouble());
// TODO: Determine larger triangle (use getArea())
Triangle largest = (triangle1.getArea() > triangle2.getArea()) ? triangle1:triangle2;
System.out.println("Triangle with larger area:");
// TODO: Output larger triangle's info (use printInfo())
largest.printInfo();
}
}

View File

@ -0,0 +1,23 @@
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int[] userList = new int[20]; // List of numElement integers specified by the user
int numElements; // Number of integers in user's list
// Add more variables as needed
int i;
numElements = scnr.nextInt(); // Input begins with number of integers that follow
/* Type your code here. */
for (i =0; i<numElements;i++){
userList[i] = scnr.nextInt();
}
for (i = numElements-1; i>-1; i--){
System.out.print(userList[i] + " ");
}
System.out.println();
}
}

View File

@ -0,0 +1,40 @@
/* Joseph Green
* 07-Oct-2020
*
* Read int that indicates list of words to follow
* Read in that many of words
* assume list will always contain less than 20 words
*/
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
/* Type your code here. */
Scanner scnr = new Scanner(System.in);
int numOfWords;
int i;
String[] listOfWords = new String[20];
int[] frequency = new int[20];
numOfWords = scnr.nextInt();
for (i=0; i<numOfWords;i++){
listOfWords[i] = scnr.next();
}
for (i=0; i<numOfWords;i++){
for (int j=0; j<numOfWords;j++){
if (listOfWords[i].equals(listOfWords[j])){
frequency[i] +=1;
}
}
}
for (i=0; i<numOfWords;i++){
System.out.println(listOfWords[i] + " " + frequency[i]);
}
}
}