CodeLingo Lesson
Lesson
public void drawLine(int n) {
for (int i = 1; i <= n; i++) {
System.out.print("*");
}
if(n > 0){
System.out.println();
drawLine(n - 1);
}
}
drawLine(10);
import java.util.ArrayList;
import java.util.Comparator;
public class Country {
private String name;
private int size;
public Country(String name, int size) {
this.name = name;
this.size = size; // in million miles ^2
}
public String getName() {
return name;
}
public int getSize() {
return size;
}
public static void main(String[] args) {
ArrayList<Country> countries = new ArrayList<>();
countries.add(new Country("USA", 3797));
countries.add(new Country("Russia", 6602));
countries.add(new Country("China", 3702));
countries.add(new Country("Canada", 3855));
// Print the unsorted countries
System.out.println("Unsorted:");
for (Country country : countries) {
System.out.print(country.getName() + ": " + country.getSize() + ", ");
}
System.out.println("");
// Sort the countries in decreasing order based on their size
countries.sort(new Comparator<Country>() {
@Override
public int compare(Country c1, Country c2) {
// Compare the sizes in descending order
return Integer.compare(c2.getSize(), c1.getSize());
}
});
// Print the sorted countries
System.out.println("Sorted:");
for (Country country : countries) {
System.out.print(country.getName() + ": " + country.getSize() + ", ");
}
}
}
Country.main(null)
public class Compare{
public boolean compareArrayLists(ArrayList<Integer> list1, ArrayList<Integer> list2){
if(list1.size() != list2.size()){
return false;
}
for(int i=0; i < list1.size(); i++){
if(list1.get(i) != list2.get(list2.size()-i-1)){
return false;
}
}
return true;
}
public void delete(ArrayList<Integer> list1){
int int1 = list1.get(0);
for(int i = 0; i < list1.size(); i++){
if (i !=0){
list1.remove(i);
}
}
}
public static void main(String[] args){
Compare compare = new Compare();
ArrayList<Integer> list1 = new ArrayList<Integer>();
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(4);
list1.add(5);
list1.add(6);
list1.add(7);
ArrayList<Integer> list2 = new ArrayList<Integer>();
list2.add(7);
list2.add(6);
list2.add(5);
list2.add(4);
list2.add(3);
list2.add(2);
list2.add(1);
System.out.println(list1);
System.out.println(list2);
System.out.println("Reversed - " + compare.compareArrayLists(list1,list2));
compare.delete(list1);
System.out.println(list1);
}
}
Compare.main(null);