Open ShivamCapg-75 opened 2 years ago
@J3-Aquacool
import java.util.ArrayList; import java.util.Collections; import java.util.List;
class Info { String Name; String House; String Role; String Status; String Dies;
Info(String Name,String House,String Role, String Status, String Dies) {
this.Name=Name;
this.House=House;
this.Role=Role;
this.Status=Status;
this.Dies=Dies;
} public String getName() { return Name; } public String getHouse() { return House; } public String getRole() { return Role; } public String getStatus() { return Status; } public String getDies() { return Dies; }
public String toString() { return Name+ " "+House+" "+Role+" "+Status+" "+Dies; }
public ArrayList getGryffindor(ArrayList
ArrayList<String> gryffindor=new ArrayList<>();
for (Info person: i) {
if (person.getHouse()=="Gryffindor") {
gryffindor.add(person.getName());
}
}
return gryffindor;
}
public ArrayList getAlive(ArrayList<Info> i)
{
ArrayList<Info> alive=new ArrayList<>();
for (Info person: i) {
if (person.getDies()=="No")
alive.add(person);
}
alive.sort((o1, o2) -> o1.getHouse().compareTo(o2.getHouse()));
return alive;
}
public ArrayList getFamily(ArrayList<Info> i)
{
ArrayList<Info> family=new ArrayList<>();
for (Info person: i) {
if (person.getStatus()=="Family") {
family.add(person);
}
}
return family;
}
public ArrayList getFaculty(ArrayList<Info> i)
{
ArrayList<Info> faculty=new ArrayList<>();
for (Info person: i) {
if (person.getDies()=="Yes"&& person.getRole()=="Faculty") {
faculty.add(person);
}
}
faculty.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));
return faculty;
}
}
public class HarryPotter {
public static void main(String[] args) {
ArrayList<Info> i =new ArrayList<Info>();
ArrayList<Info> result2=new ArrayList<Info>();
ArrayList<Info> result3=new ArrayList<Info>();
ArrayList<String> result1,result4 = new ArrayList<String>();
i.add(new Info("Harry Potter","Gryffindor ","Student","Self","No"));
i.add(new Info("Ginny Weasley","Gryffindor ","Student","Friend ","No"));
i.add(new Info("Ron Weasley","Gryffindor ","Student","Friend","No"));
i.add(new Info("Hermione Granger","Gryffindor ","Student","Friend ","No"));
i.add(new Info("Neville Longbottom","Gryffindor ","Student","Friend","No"));
i.add(new Info("Oliver Wood","Gryffindor ","Student","Friend","No"));
i.add(new Info("Luna Lovegood","Ravenclaw","Student","Friend","No"));
i.add(new Info("Cho Chang","Ravenclaw","Student","Friend","No"));
i.add(new Info("Cedric Diggory","Hufflepuff","Student","Friend","Yes"));
i.add(new Info("Hannah Abbot","Hufflepuff","Student","Friend","No"));
i.add(new Info("Draco Malfoy","Slytherin","Student","Enemy","No"));
i.add(new Info("Vincent Crabbe","Slytherin","Student","Enemy","Yes"));
i.add(new Info("Gregory Goyle","Slytherin","Student","Enemy","No"));
i.add(new Info("Penelope Clearwater","Slytherin","Student","Enemy","No"));
i.add(new Info("Albus Dumbledore","Gryffindor ","Faculty","Friend","Yes"));
i.add(new Info("Severus Snape","Slytherin ","Faculty","Enemy","Yes"));
i.add(new Info("Remus Lupin","Gryffindor ","Faculty","Friend","Yes"));
i.add(new Info("Horace Slughorn","Slytherin ","Faculty","Friend","No"));
i.add(new Info("Rubeus Hagrid","Gryffindor ","Faculty","Friend","No"));
i.add(new Info("Minerva McGonagall","Gryffindor","Faculty","Friend","No"));
i.add(new Info("James Potter","Gryffindor","Student","Family","Yes"));
i.add(new Info("Sirius Black","Gryffindor","Student","Family","Yes"));
i.add(new Info("Lily Potter","Gryffindor","Student","Family","Yes"));
i.add(new Info("Peter Pettigrew","Gryffindor","Student","Enemy","Yes"));
i.add(new Info("Tom Marvolo Riddle","Slytherin","Student","Enemy","Yes"));
Info obj=new Info(null, null, null, null, null);
result1=obj.getGryffindor(i);
System.out.println("People who are from gryffindor are: " +result1);
result2=obj.getAlive(i);
System.out.println("\nPeople who are alive are : " +result2);
result3=obj.getFamily(i);
System.out.println("\nFamily members of Harry Potter are : " +result3);
result4=obj.getFaculty(i);
System.out.println("\nFaculty who died are : " +result4);
} }
@J3-Aquacool
2.Create a Collection of the type ArrayList and store Vehicle object.
Vehicle object has the following attributes
vname brand price
show us the sorting of Vehicle by name,price and brand
//import java.util.; //import java.io.; //import java.math.; //import java.util.regex.; //import java.text.*;
class UserDefinedException { public static void main(String args[]) { System.out.println("Shivam HDFC Bank Account\n" ); Account acct = new Account(); System.out.println("Current balance : " + acct.balance()); System.out.println("Withdrawing Amount 200"); acct.withdraw(200); System.out.println("Current balance : " + acct.balance()); acct.withdraw(1000); } } class Account { private int balance = 1000; public int balance() { return balance; } public void withdraw(int amount) throws NotSufficientFundException { if (amount > balance) { throw new NotSufficientFundException(String.format("Current balance %d is less than requested amount %d", balance, amount)); } balance = balance - amount; } public void deposit(int amount) { if (amount <= 0) { throw new IllegalArgumentException( String.format("Invalid deposit amount %s", amount)); } } } class NotSufficientFundException extends RuntimeException { private String message; public NotSufficientFundException(String message) { this.message = message; } public NotSufficientFundException(Throwable cause, String message) { super(cause); this.message = message; } public String getMessage() { return message; } }
// Java Program to Sort an ArrayList
import java.util.*;
class Car { String Vname; String Brand; int Price;
}
class PriceComparator implements Comparator {
}
class GFG {
}