Skip to content

Instantly share code, notes, and snippets.

@atwalsh
Last active August 29, 2015 14:18
Show Gist options
  • Save atwalsh/fc47a3ac647710560ec7 to your computer and use it in GitHub Desktop.
Save atwalsh/fc47a3ac647710560ec7 to your computer and use it in GitHub Desktop.
ITCS 1213 Lab 8

ITCS 1213 Lab 8

/**
*
* @author adamwalsh
*/
import java.util.*;
public class ArrayWork {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//a
int[] num = new int[10];
for(int i = 0; i<10; i++){
num[i] = i;
System.out.print(num[i]+ " ");
}
//b
System.out.println("\n");
for(int i=9; i>-1; --i){
System.out.print(num[i]+ " ");
}
//c
System.out.println("\n");
for(int i=1; i<10; i+=2){
System.out.print(num[i]+ " ");
}
//d
System.out.println("\n");
ArrayList<Integer> arrlist = new ArrayList<>(10);
for(int i=0; i<10; i++){
arrlist.add(i);
}
arrlist.stream().forEach((number) -> {
System.out.print(number+ " ");
});
System.out.println("\n");
arrlist.remove(4);
arrlist.add(0);
arrlist.stream().forEach((number) -> {
System.out.print(number+ " ");
});
//e
System.out.print("\n");
arrlist.remove(2);
arrlist.add(0);
arrlist.stream().forEach((number) -> {
System.out.print(number+ " ");
});
//f
System.out.print("\n");
int[] num2 = Arrays.copyOf(num, 20);
for(int i = 0; i<num2.length; i++){
System.out.print(num2[i]+ " ");
}
}
}
import java.util.*;
/**
*
* @author adamwalsh
*/
public class Driver {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Invoice I1;
Invoice I2;
String[] inv1;
String[] inv2;
//get invoice data
System.out.println("Format: ID, description, amount, is paid (bool)");
System.out.println("Enter first Invoice:");
inv1 = input.nextLine().split(",");
double amt1 = Double.parseDouble(inv1[2]);
boolean paid1 = Boolean.parseBoolean(inv1[3]);
I1 = new Invoice(inv1[0], inv1[1], amt1, paid1);
System.out.println("Enter second Invoice:");
inv2 = input.nextLine().split(",");
double amt2 = Double.parseDouble(inv2[2]);
boolean paid2 = Boolean.parseBoolean(inv2[3]);
I2 = new Invoice(inv2[0], inv2[1], amt2, paid2);
//print invoices
System.out.println("\nInvoices: ");
for (String inv11 : inv1) {
System.out.print(inv11 + " ");
}
System.out.println("\n");
for (String inv21 : inv2) {
System.out.print(inv21 + " ");
}
//check if same amount
System.out.println("\n");
if (I1.compareTo(I2) == 1)
System.out.print("Invoices have the same amount");
else if (I1.compareTo(I2) == -1)
System.out.println("Invoices have different amounts");
//check if same ID
if(I1.equals(I2))
System.out.println("Instances have same ID");
else
System.out.println("Instances have different ID");
}
}
/**
*
* @author adamwalsh
*/
public class Invoice {
private String invoiceID, description;
private double amount;
private boolean isPaid;
public Invoice(String i, String d, double a, boolean isP){
invoiceID = i;
description = d;
amount = a;
isPaid = isP;
}
public boolean equals(Invoice otherInvoice){
if(this == otherInvoice)
return true;
if(otherInvoice instanceof Invoice) {
Invoice that = (Invoice) otherInvoice;
return (invoiceID == null && that.invoiceID == null)
|| invoiceID.equals(that.invoiceID);
}
return false;
}
public int compareTo(Invoice otherInvoice){
if (this.getAmount() == otherInvoice.getAmount())
return 1;
else
return -1;
}
/**
* @param invoiceID the invoiceID to set
*/
public void setInvoiceID(String invoiceID) {
this.invoiceID = invoiceID;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @param amount the amount to set
*/
public void setAmount(double amount) {
this.amount = amount;
}
/**
* @param isPaid the isPaid to set
*/
public void setIsPaid(boolean isPaid) {
this.isPaid = isPaid;
}
/**
* @return the amount
*/
public double getAmount() {
return amount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment