From ff22d459939b19ab31c82efe30d667d4cd97fba2 Mon Sep 17 00:00:00 2001 From: jgaonap Date: Sat, 14 Jul 2018 09:08:49 -0500 Subject: [PATCH] =?UTF-8?q?se=20a=C3=B1aden=20los=20ejercicios=20realizado?= =?UTF-8?q?s=20hasta=20el=20momento=20jgaona=2014/07/2018?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ex03_1_exercise/ShoppingCart.java | 16 ++++++ .../ex04_1_exercise/ShoppingCart.java | 26 ++++++++++ .../ex04_2_exercise/ShoppingCart.java | 36 ++++++++++++++ .../ex05_1_exercise/ShoppingCart.java | 43 ++++++++++++++++ .../ex05_2_exercise/ShoppingCart.java | 35 +++++++++++++ .../ex05_3_exercise/ShoppingCart.java | 35 +++++++++++++ .../exercices/ex06_1_exercise/Item.java | 17 +++++++ .../exercices/ex06_2_exercise/Item.java | 19 +++++++ .../ex06_2_exercise/ShoppingCart.java | 35 +++++++++++++ .../ex07_1_exercise/ShoppingCart.java | 24 +++++++++ .../exercices/ex08_1_exercise/Item.java | 18 +++++++ .../ex08_1_exercise/ShoppingCart.java | 22 +++++++++ .../exercices/ex08_2_exercise/Item.java | 36 ++++++++++++++ .../ex08_2_exercise/ShoppingCart.java | 25 ++++++++++ .../exercices/ex09_1_exercise/Customer.java | 24 +++++++++ .../exercices/ex09_2_exercise/Customer.java | 25 ++++++++++ .../ex09_2_exercise/ShoppingCart.java | 15 ++++++ .../exercices/ex10_1_exercise/TestClass.java | 21 ++++++++ .../exercices/ex10_2_exercise/Order.java | 49 +++++++++++++++++++ .../ex10_2_exercise/ShoppingCart.java | 9 ++++ .../exercices/ex10_3_exercise/Order.java | 45 +++++++++++++++++ .../ex10_3_exercise/ShoppingCart.java | 9 ++++ 22 files changed, 584 insertions(+) create mode 100644 src/main/java/jorgeantonio/exercices/ex03_1_exercise/ShoppingCart.java create mode 100644 src/main/java/jorgeantonio/exercices/ex04_1_exercise/ShoppingCart.java create mode 100644 src/main/java/jorgeantonio/exercices/ex04_2_exercise/ShoppingCart.java create mode 100644 src/main/java/jorgeantonio/exercices/ex05_1_exercise/ShoppingCart.java create mode 100644 src/main/java/jorgeantonio/exercices/ex05_2_exercise/ShoppingCart.java create mode 100644 src/main/java/jorgeantonio/exercices/ex05_3_exercise/ShoppingCart.java create mode 100644 src/main/java/jorgeantonio/exercices/ex06_1_exercise/Item.java create mode 100644 src/main/java/jorgeantonio/exercices/ex06_2_exercise/Item.java create mode 100644 src/main/java/jorgeantonio/exercices/ex06_2_exercise/ShoppingCart.java create mode 100644 src/main/java/jorgeantonio/exercices/ex07_1_exercise/ShoppingCart.java create mode 100644 src/main/java/jorgeantonio/exercices/ex08_1_exercise/Item.java create mode 100644 src/main/java/jorgeantonio/exercices/ex08_1_exercise/ShoppingCart.java create mode 100644 src/main/java/jorgeantonio/exercices/ex08_2_exercise/Item.java create mode 100644 src/main/java/jorgeantonio/exercices/ex08_2_exercise/ShoppingCart.java create mode 100644 src/main/java/jorgeantonio/exercices/ex09_1_exercise/Customer.java create mode 100644 src/main/java/jorgeantonio/exercices/ex09_2_exercise/Customer.java create mode 100644 src/main/java/jorgeantonio/exercices/ex09_2_exercise/ShoppingCart.java create mode 100644 src/main/java/jorgeantonio/exercices/ex10_1_exercise/TestClass.java create mode 100644 src/main/java/jorgeantonio/exercices/ex10_2_exercise/Order.java create mode 100644 src/main/java/jorgeantonio/exercices/ex10_2_exercise/ShoppingCart.java create mode 100644 src/main/java/jorgeantonio/exercices/ex10_3_exercise/Order.java create mode 100644 src/main/java/jorgeantonio/exercices/ex10_3_exercise/ShoppingCart.java diff --git a/src/main/java/jorgeantonio/exercices/ex03_1_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercices/ex03_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..cfdc922 --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex03_1_exercise/ShoppingCart.java @@ -0,0 +1,16 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package ex03_1_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main (String[] args){ + System.out.println("Este es mi mensaje"); + } +} diff --git a/src/main/java/jorgeantonio/exercices/ex04_1_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercices/ex04_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..dbca7c4 --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex04_1_exercise/ShoppingCart.java @@ -0,0 +1,26 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package ex04_1_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main(String[] args) { + // Declare and initialize String variables. Do not initialize message yet. + String custName = "Jorge ", itemDesc = "loves the music"; + String message; + + + // Assign the message variable + message = custName + itemDesc; + + // Print and run the code + + System.out.println(message); + } +} diff --git a/src/main/java/jorgeantonio/exercices/ex04_2_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercices/ex04_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..2266172 --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex04_2_exercise/ShoppingCart.java @@ -0,0 +1,36 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package ex04_2_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main(String[] args) { + String custName = "Jorge"; + String itemDesc = "Discs"; + String message = custName+" bought "+itemDesc; + double price = 52.0 , tax = 5.2; + int quantity = 2; + double total; + // Declare and initialize numeric fields: price, tax, quantity, and total. + + + // Modify message to include quantity + message = custName + " bought " + quantity + " " + itemDesc; + + System.out.println(message); + + total = price * quantity * tax; + + + // Calculate total and then print the total cost + + System.out.println("and they cost " + total); + + } +} diff --git a/src/main/java/jorgeantonio/exercices/ex05_1_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercices/ex05_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..d90aa18 --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex05_1_exercise/ShoppingCart.java @@ -0,0 +1,43 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package ex05_1_exercise; +public class ShoppingCart { + + public static void main(String[] args) { + String custName = "Mary Smith"; + String itemDesc = "Shirt"; + + // numeric fields + double price = 21.99; + int quantity = 2; + double tax = 1.04; + double total; + String message = custName+" wants to purchase "+quantity+" "+itemDesc; + + // Calculating total cost + total = (price*quantity)*tax; + + + // Declare outOfStock variable and initialize it + boolean outOfStock = true; + + // Test quantity and modify message if quantity > 1. + if(quantity > 1){ + message = message +"s"; + } + + // Test outOfStock and notify user in either case. + + if(outOfStock == true){ + System.out.println("The item unavailable"); + }else{ + System.out.println(message); + System.out.println("And the total is " + total); + } + + } + +} diff --git a/src/main/java/jorgeantonio/exercices/ex05_2_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercices/ex05_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..ecadef0 --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex05_2_exercise/ShoppingCart.java @@ -0,0 +1,35 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package ex05_2_exercise; + +/** + * + * @author Hp + */ + +public class ShoppingCart { + + public static void main(String[] args) { + // local variables + String custName = "Mary Smith"; + String message = custName + " wants to purchase a several items."; + + //Declare and initialize the items String array + String items [] = new String[]{"Shirt","Jeans","Shoes",""}; + + + + // Change message to show the number of items purchased. + + message = "The items in total are " + items.length; + + System.out.println(message); + // Print an element from the items array. + System.out.print(items[0]+"\n"); + + } + +} \ No newline at end of file diff --git a/src/main/java/jorgeantonio/exercices/ex05_3_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercices/ex05_3_exercise/ShoppingCart.java new file mode 100644 index 0000000..635cef9 --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex05_3_exercise/ShoppingCart.java @@ -0,0 +1,35 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package ex05_3_exercise; + +public class ShoppingCart { + + public static void main(String[] args) { + // local variables + String custName = "Mary Smith"; + String message; + double price = 21.99; + int quantity = 2; + double tax = 1.04; + + String items[]; + items = new String[4]; + items[0] = "Shirt"; + items[1] = "Belt"; + items[2] = "Scarf"; + items[3] = "Skirt"; + + message = custName + " wants to purchase "+items.length+" items."; + System.out.println(message); + + for (int i = 0; i < items.length; i++) { + System.out.println(items[i]); + } + + + } +} + diff --git a/src/main/java/jorgeantonio/exercices/ex06_1_exercise/Item.java b/src/main/java/jorgeantonio/exercices/ex06_1_exercise/Item.java new file mode 100644 index 0000000..c30b849 --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex06_1_exercise/Item.java @@ -0,0 +1,17 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package ex06_1_exercise; + +/** + * + * @author Hp + */ +public class Item { + public int id; + public String descr; + public int quantity; + public double price; +} diff --git a/src/main/java/jorgeantonio/exercices/ex06_2_exercise/Item.java b/src/main/java/jorgeantonio/exercices/ex06_2_exercise/Item.java new file mode 100644 index 0000000..284edde --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex06_2_exercise/Item.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package ex06_2_exercise; + + + +/** + * + * @author Hp + */ +public class Item { + public int itemId; + public String descr; + public int quantity; + public double price; +} diff --git a/src/main/java/jorgeantonio/exercices/ex06_2_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercices/ex06_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..bde1d4a --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex06_2_exercise/ShoppingCart.java @@ -0,0 +1,35 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package ex06_2_exercise; + +public class ShoppingCart { + + public static void main(String args[]) { + // Declare and initialize 2 Item objects + Item item1 = new Item(); + Item item2 = new Item(); + + item1.itemId = 1; + item1.descr = "Playera de Brasil"; + item1.price = 500.00; + item1.quantity = 5; + + item2.itemId = 1; + item2.descr = "Playera de Mexico"; + item2.price = 1150.00; + item2.quantity = 5; + + // Print both item descriptions and run code. + System.out.println("The first description is " + item1.descr + "\n" + + "The second description is " + item2.descr); + + item1 = item2; + + System.out.println("The new description is " + item1.descr); + + } + +} \ No newline at end of file diff --git a/src/main/java/jorgeantonio/exercices/ex07_1_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercices/ex07_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..475e610 --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex07_1_exercise/ShoppingCart.java @@ -0,0 +1,24 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package ex07_1_exercise; + +public class ShoppingCart { + public static void main (String[] args){ + String custName = "Steve Smith"; + String firstName; + int spaceIdx; + + // Get the index of the space character (" ") in custName. + spaceIdx = custName.indexOf(" "); + + + // Use the substring method parse out the first name and print it. + firstName = custName.substring(0,spaceIdx); + + System.out.println(firstName); + + } +} \ No newline at end of file diff --git a/src/main/java/jorgeantonio/exercices/ex08_1_exercise/Item.java b/src/main/java/jorgeantonio/exercices/ex08_1_exercise/Item.java new file mode 100644 index 0000000..9905808 --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex08_1_exercise/Item.java @@ -0,0 +1,18 @@ + +package ex08_1_exercise; + +public class Item { + char color; + + public boolean SetColor(char colorCode){ + if(colorCode == ' '){ + return false; + }else{ + this.color = colorCode; + return true; + } + + } + + +} diff --git a/src/main/java/jorgeantonio/exercices/ex08_1_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercices/ex08_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..ced2c26 --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex08_1_exercise/ShoppingCart.java @@ -0,0 +1,22 @@ + +package ex08_1_exercise; + +public class ShoppingCart { + public static void main(String[] args){ + Item item1 = new Item(); + if(true == item1.SetColor('e')){ + System.out.println("THE NEW COLOR IS " + item1.color); + }else{ + System.out.println("INVALID COLOR"); + } + + // Call the setColor method on item1. Print the new color value from + // item1 if the method returns true. Otherwise print an "invalid color" + // message. + + + // Test the class, using both valid and invalid colors. + + + } +} diff --git a/src/main/java/jorgeantonio/exercices/ex08_2_exercise/Item.java b/src/main/java/jorgeantonio/exercices/ex08_2_exercise/Item.java new file mode 100644 index 0000000..c7655e2 --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex08_2_exercise/Item.java @@ -0,0 +1,36 @@ + +package ex08_2_exercise; + + +public class Item { + String desc; + int quantity; + double price; + char colorCode = 'U'; //'U' = Undetermined + + public void displayItem() { + System.out.println("Item: " + desc + ", " + quantity + ", " + + price + ", "+colorCode); + } + + // Write a public 3-arg setItemDisplay method that returns void. + public void setItemFields(String desc, int quantity, double price){ + this.desc = desc; + this.quantity = quantity; + this.price = price; + } + public int setItemFields(String desc, int quantity, double price,char colorCode){ + if(colorCode == ' '){ + return-1; + }else{ + this.colorCode = colorCode; + setItemFields(desc, quantity, price); + return 1; + } + } + + // Write a public 4-arg setItemDisplay method that returns an int. + + + +} diff --git a/src/main/java/jorgeantonio/exercices/ex08_2_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercices/ex08_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..8ca73ff --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex08_2_exercise/ShoppingCart.java @@ -0,0 +1,25 @@ + +package ex08_2_exercise; + + +public class ShoppingCart { + public static void main(String[] args) { + Item item1 = new Item(); + + // Call the 3-arg setItemFields method and then call displayItem. + item1.setItemFields("Saco Negro", 5, 250.00); + item1.displayItem(); + + // Call the 4-arg setItemFields method, checking the return value. + + if(item1.setItemFields("Playera Gris", 50, 100.00, 'B') > 0){ + item1.displayItem(); + }else{ + System.out.println("Invalid Color"); + } + // Test your code for both valid and invalid values + + + + } +} diff --git a/src/main/java/jorgeantonio/exercices/ex09_1_exercise/Customer.java b/src/main/java/jorgeantonio/exercices/ex09_1_exercise/Customer.java new file mode 100644 index 0000000..b93f775 --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex09_1_exercise/Customer.java @@ -0,0 +1,24 @@ +package ex09_1_exercise; + + +public class Customer { + private String name; + private String ssn; + + + // Encapsulate this class. Make ssn read only + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSsn() { + return ssn; + } + + + +} diff --git a/src/main/java/jorgeantonio/exercices/ex09_2_exercise/Customer.java b/src/main/java/jorgeantonio/exercices/ex09_2_exercise/Customer.java new file mode 100644 index 0000000..58facb4 --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex09_2_exercise/Customer.java @@ -0,0 +1,25 @@ +package ex09_2_exercise; + + +public class Customer { + private String name; + private String ssn; + + //Add a custom constructor + public Customer(String name, String ssn){ + this.name = name; + this.ssn = ssn; + } + + + public String getName(){ + return name; + } + public void setName(String n){ + name = n; + } + + public String getSSN(){ + return ssn; + } +} diff --git a/src/main/java/jorgeantonio/exercices/ex09_2_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercices/ex09_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..3f7b8fb --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex09_2_exercise/ShoppingCart.java @@ -0,0 +1,15 @@ +package ex09_2_exercise; + + +public class ShoppingCart { + + public static void main(String args[]) { + // Declare, instantiate, and initialize a Customer object + Customer customer = new Customer("Jorge Gaona Puch","JGP1234"); + + System.out.println(customer.getName()); + // Print the customer object name + + + } +} diff --git a/src/main/java/jorgeantonio/exercices/ex10_1_exercise/TestClass.java b/src/main/java/jorgeantonio/exercices/ex10_1_exercise/TestClass.java new file mode 100644 index 0000000..048cd80 --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex10_1_exercise/TestClass.java @@ -0,0 +1,21 @@ + +package ex10_1_exercise; + + +public class TestClass { + + public static void main(String args[]) { + int x = 4, y = 9; + if (y / x < 3) { + x += y; + } else { + x *= y; + } + System.out.println("After if stmt, x = " + x); + + int ternario = (y / x < 3) ? (x += y) : (x *= y); + + System.out.println("After ternary stmt, x = " + x); + // Use a ternary operator to perform the same logic as above. + } +} diff --git a/src/main/java/jorgeantonio/exercices/ex10_2_exercise/Order.java b/src/main/java/jorgeantonio/exercices/ex10_2_exercise/Order.java new file mode 100644 index 0000000..d5a59e6 --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex10_2_exercise/Order.java @@ -0,0 +1,49 @@ + +package ex10_2_exercise; + +public class Order { + + static final char CORP = 'C'; + static final char PRIVATE = 'P'; + static final char NONPROFIT = 'N'; + String name; + double total; + String stateCode; + double discount; + char custType; + + public Order(String name, double total, String state, char custType) { + this.name = name; + this.total = total; + this.stateCode = state; + this.custType = custType; + calcDiscount(); + } + + public String getDiscount(){ + return Double.toString(discount) + "%"; + } + + // Code the calcDiscount method. + public void calcDiscount() { + if(custType == 'N'){ + if (total > 900) { + discount = 10.00; + } else { + discount = 5.00; + } + }else if(custType == 'P'){ + if (total > 900) { + discount = 7.00; + } else { + discount = 0.00; + } + }else if(custType == 'C'){ + if (total < 500) { + discount = 8.00; + } else { + discount = 5.00; + } + } + } +} diff --git a/src/main/java/jorgeantonio/exercices/ex10_2_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercices/ex10_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..62a114a --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex10_2_exercise/ShoppingCart.java @@ -0,0 +1,9 @@ + +package ex10_2_exercise; + +public class ShoppingCart { + public static void main(String args[]){ + Order order = new Order("Rick Wilson", 910.00, "VA", Order.NONPROFIT); + System.out.println("Discount is: "+ order.getDiscount()); + } +} diff --git a/src/main/java/jorgeantonio/exercices/ex10_3_exercise/Order.java b/src/main/java/jorgeantonio/exercices/ex10_3_exercise/Order.java new file mode 100644 index 0000000..6da432e --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex10_3_exercise/Order.java @@ -0,0 +1,45 @@ + +package ex10_3_exercise; + +public class Order { + static final char CORP = 'C'; + static final char PRIVATE = 'P'; + static final char NONPROFIT = 'N'; + String name; + double total; + String stateCode; + double discount; + char custType; + + public Order(String name, double total, String state, char custType) { + this.name = name; + this.total = total; + this.stateCode = state; + this.custType = custType; + calcDiscount(); + } + + public String getDiscount(){ + return Double.toString(discount) + "%"; + } + + public void calcDiscount() { + // Replace the if logic with a switch statement. + + switch(custType){ + case CORP: + discount = (total > 500) ? 8.00 : 5.00; + break; + case PRIVATE: + discount = (total > 900) ? 7.00 : 0.00; + break; + case NONPROFIT: + discount = (total > 900) ? 10.00 : 5.00; + break; + default: + discount = 0.0; + break; + + } + } +} diff --git a/src/main/java/jorgeantonio/exercices/ex10_3_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercices/ex10_3_exercise/ShoppingCart.java new file mode 100644 index 0000000..5451e20 --- /dev/null +++ b/src/main/java/jorgeantonio/exercices/ex10_3_exercise/ShoppingCart.java @@ -0,0 +1,9 @@ + +package ex10_3_exercise; + +public class ShoppingCart { + public static void main(String args[]){ + Order order = new Order("Rick Wilson", 910.00, "VA", Order.NONPROFIT); + System.out.println("Discount is: "+ order.getDiscount()); + } +}