Units 6-10 Homework
// HACK!!!!
// Create an arrayList and use one of the cool methods for it
import java.util.ArrayList;
public class hack1 {
public static void main (String[] args) {
ArrayList<String>
color.add(red);
color.add(blue);
}
}
hack1.main(null);
import java.util.ArrayList;
public class main{
public static void main(String[] args) {
ArrayList<String> color = new ArrayList<String>();
color.add("red apple");
color.add("green box");
color.add("blue water");
color.add("red panda");
for (int i = 0; i < color.size(); i++) {
if (color.get(i).contains("red")) {
color.remove(i);
}
}
for (int i = 0; i < color.size(); i++){
System.out.println(color.get(i) + " ");
}
}
}
// Hack #3
// find the sum of the elements in the arraylist
ArrayList<Integer> num = new ArrayList<Integer>();
num.add(5);
num.add(1);
num.add(3);
int count = 0;
for ( int n : num) {
count = count + n;
}
System.out.print(count);
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "a", "f", "g" },
{ "b", "e", "h" },
{ "c", "d", "i" }
};
// Print the last element in the array!
System.out.println(arr[arr. length-1][arr[0]. length-1]);
}
}
Test.main(null);
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
// Change Austin to Athens and print!
arr[2][0] = "Athens";
System.out.println(arr[2][0]);
}
}
Test.main(null);
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
// Print out the array without using numerical values!
int r = arr.length;
int c = arr[0].length;
for (int row = 0; row < r; row++) {
for (int col = 0; col < c; col++){
System.out.print(arr[row][col] + ", " );
}
}
}
}
Test.main(null);
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
String longest = arr[0][0];
// Use nested for loops to find the longest or shortest string!
int r = arr.length;
int c = arr[0].length;
for (int row = 0; row < r; row++){
for (int col=0; col<c; col++){
if(arr[row][col].length()> longest.length()){
longest = arr[row][col];
}
}
}
System.out.print(longest);
}
}
Test.main(null);
public class Tree {
public static voild main(String args[]) {
String[][] tree = {
{" " ," ", " ", "*", " ", " ", " "}
{" " ," ", "*", "*", "*", " ", " "}
{" " ,"*", "*", "*", "*", "*", " "}
{"*" ,"*", "*", "*", "*", "*", "*"}
{" " ," ", " ", "M", " ", " ", " "}
};
int r = tree.length;
int c = tree[0].length;
for (int row = 0; row < r; row++){
for (int col = 0; col < c; col++){
System.out.print(tree[row][col]);
}
System.out.println(" ");
}
}
}
Tree.main(null);
public class Candy {
protected double candyCalories;
protected double candyRating;
protected double candySize;
public Candy(double candyCalories, double candySize, double candyRating){
this.candyCalories = candyCalories;
this.candyRating = candyRating;
this.candySize = candySize;
}
public void healthWarning() {
System.out.println("Candy is not healthy. Consume in moderation!");
}
public static void main(String[] args){
Candy can = new Candy(100, 20, 9.4);
can.healthWarning();
}
}
Candy.main(null);
public class Starbursts extends Candy{
protected String candyColor;
public Starbursts(double candyCalories, double candySize, double candyRating, String candyColor){
super(candyCalories, candySize, candyRating);
this.candyColor = candyColor;
}
@Override
public void healthWarning() {
System.out.println("Starbursts are not good for you!!!");
}
public void type(String type1){
System.out.println("Your candy is " + type1 + ".");
}
public void type(String type1, String type2) {
System.out.println("Your candy is " + type1 + " and " + type2 + ".");
}
public static void main(String[] args){
Starbursts star = new Starbursts(20, 0.5, 6.5, "Red");
star.healthWarning();
star.type("sweet");
star.type("sweet", "chewy");
}
}
Starbursts.main(null);
public class Smarties extends Candy{
protected String candyColor;
public Smarties(double candyCalories, double candySize, double candyRating, String candyColor){
super(candyCalories, candySize, candyRating);
this.candyColor = candyColor;
}
@Override
public void healthWarning() {
System.out.println("Smarties are not good for you!!!");
}
public static void main(String[] args){
Smarties smart = new Smarties(20, 0.5, 6.5, "Red");
smart.healthWarning();
}
}
Smarties.main(null);
public class output {
public static int foo(int a, int b){
if(b<=1||b<=a){
return 1;
}
return(b-a)*foo(a,b-1);
}
public static void main(String[] args){
System.out.println(foo(5,9));
}
}
output.main(null);