Task - Multithreading - High Mid Low #42
Replies: 30 comments
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`package MultiThreading; import java.util.ArrayList; class Height{ } abstract class Student{ } class EEEStudent extends Student{ } class ECEStudent extends Student{ } class CSEStudent extends Student{ } class StudentAdministration{ } public class MultiThreadingStudents { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
import java.util.ArrayList; class Height { } abstract class Student { } class EEEStudent extends Student { } class ECEStudent extends Student { } class CSEStudent extends Student { } class StudentAdministration { } public class UniversityDriver { } |
Beta Was this translation helpful? Give feedback.
-
|
import java.util.ArrayList; class Height { } abstract class Student { } class EEEStudent extends Student { } class ECEStudent extends Student { } class CSEStudent extends Student { } class StudentAdministration { } public class UniversityDriver { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`package week5; import java.util.ArrayList; class Height { } abstract class Student { } class EEEStudent extends Student { } class ECEStudent extends Student { } class CSEStudent extends Student { } class StudentAdministration { } public class UniversityDriverClass { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
import java.util.ArrayList; } |
Beta Was this translation helpful? Give feedback.
-
|
`class HeightTwo { } abstract class StudentTwo { } class EEEStudent extends StudentTwo { } class ECEStudent extends StudentTwo { } class CSEStudent extends StudentTwo { } class StudentAdministration { } public class UniversityDriver { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
import java.util.ArrayList; class Height { } abstract class Student { } class EEEStudent extends Student { } class ECEStudent extends Student { } class CSEStudent extends Student { } class StudentAdministration { } public class UniversityDriver { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
package Serialization;
import java.util.ArrayList;
class HeightTwo {
private int feet;
private int inches;
public HeightTwo(int feet, int inches) {
this.feet = feet;
this.inches = inches;
}
public int getFeet() {
return feet;
}
public void setFeet(int feet) {
this.feet = feet;
}
public int getInches() {
return inches;
}
public void setInches(int inches) {
this.inches = inches;
}
}
abstract class StudentTwo {
private String name;
private String branch;
private HeightTwo height;
public StudentTwo(String name, String branch, HeightTwo height) {
this.name = name;
this.branch = branch;
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public HeightTwo getHeight() {
return height;
}
public void setHeight(HeightTwo height) {
this.height = height;
}
public static int compareByHeight(StudentTwo s1, StudentTwo s2) {
if (s1.height.getFeet() == s2.height.getFeet()) {
if (s1.height.getInches() == s2.height.getInches())
return 0;
else if (s1.height.getInches() > s2.height.getInches())
return 1;
else
return -1;
} else if (s1.height.getFeet() > s2.height.getFeet())
return 1;
return -1;
}
@Override
public String toString() {
return String.format("Name: %s %nBranch: %s %nHeight: %dft. %din. %n", this.name, this.branch,
this.height.getFeet(), this.height.getInches());
}
}
class EEEStudent extends StudentTwo {
public EEEStudent(String name, HeightTwo height) {
super(name, "EEE", height);
}
}
class ECEStudent extends StudentTwo {
public ECEStudent(String name, HeightTwo height) {
super(name, "ECE", height);
}
}
class CSEStudent extends StudentTwo {
public CSEStudent(String name, HeightTwo height) {
super(name, "CSE", height);
}
}
class StudentAdministration {
StudentTwo[] students;
public StudentAdministration(StudentTwo[] students) {
this.students = students;
}
public StudentTwo[] getStudents() {
return students;
}
public void setStudents(StudentTwo[] students) {
this.students = students;
}
public void sortByHeight() {
for (int k = 0; k < 4; k++) {
for (int i = 0; i < this.students.length - 1; i++) {
if (StudentTwo.compareByHeight(this.students[i], this.students[i + 1]) == -1) {
StudentTwo temp = this.students[i];
this.students[i] = this.students[i + 1];
this.students[i + 1] = temp;
}
}
}
}
public void displaySortedEEEStudents() {
ArrayList<StudentTwo> tempStudents = new ArrayList<StudentTwo>();
for (int i = 0; i < this.students.length; i++) {
if (this.students[i].getBranch().equals("EEE")) {
tempStudents.add(this.students[i]);
}
}
StudentTwo[] eeeStudents = tempStudents.toArray(new EEEStudent[tempStudents.size()]);
synchronized (this) {
for (StudentTwo s : eeeStudents) {
notifyAll();
System.out.println(s);
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void displaySortedECEStudents() {
ArrayList<StudentTwo> tempStudents = new ArrayList<StudentTwo>();
for (int i = 0; i < this.students.length; i++) {
if (this.students[i].getBranch().equals("ECE")) {
tempStudents.add(this.students[i]);
}
}
StudentTwo[] eceStudents = tempStudents.toArray(new ECEStudent[tempStudents.size()]);
synchronized (this) {
for (StudentTwo s : eceStudents) {
System.out.println(s);
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
notifyAll();
}
}
}
public void displaySortedCSEStudents() {
ArrayList<StudentTwo> tempStudents = new ArrayList<StudentTwo>();
for (int i = 0; i < this.students.length; i++) {
if (this.students[i].getBranch().equals("CSE")) {
tempStudents.add(this.students[i]);
}
}
StudentTwo[] cseStudents = tempStudents.toArray(new CSEStudent[tempStudents.size()]);
synchronized (this) {
for (StudentTwo s : cseStudents) {
notifyAll();
System.out.println(s);
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public class MultiThreadingExample {
public static void main(String[] args) {
StudentTwo[] students = new StudentTwo[6];
students[0] = new ECEStudent("Vignesh", new HeightTwo(5, 5));
students[1] = new ECEStudent("Vicky", new HeightTwo(5, 6));
students[2] = new EEEStudent("Vishwa", new HeightTwo(5, 1));
students[3] = new EEEStudent("Ronald", new HeightTwo(6, 2));
students[4] = new CSEStudent("Saran", new HeightTwo(5, 7));
students[5] = new CSEStudent("Neo", new HeightTwo(5, 9));
StudentAdministration studentAdministration = new StudentAdministration(students);
studentAdministration.sortByHeight();
new Thread() {
public void run() {
studentAdministration.displaySortedEEEStudents();
}
}.start();
new Thread() {
public void run() {
studentAdministration.displaySortedECEStudents();
}
}.start();
new Thread() {
public void run() {
studentAdministration.displaySortedCSEStudents();
}
}.start();
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.ArrayList;
class Height {
int feet;
int inches;
public Height(int feet, int inches) {
this.feet = feet;
this.inches = inches;
}
@Override
public String toString() {
return "Height [feet=" + feet + ", inches=" + inches + "]";
}
}
abstract class Student {
String name;
Height height;
String branch;
Student() {
}
public Student(String name, Height height) {
this.name = name;
this.height = height;
}
public int compareByHeight(Student s1, Student s2) {
if (s1.height.feet > s2.height.feet) {
return 1;
} else if (s1.height.feet < s2.height.feet) {
return -1;
} else {
if (s1.height.inches < s2.height.inches) {
return -1;
} else if (s1.height.feet > s2.height.feet) {
return 1;
} else {
return 0;
}
}
}
@Override
public String toString() {
return "Student [branch=" + branch + ", height=" + height + ", name=" + name + "]";
}
}
class EEEStudent extends Student {
EEEStudent() {
}
public EEEStudent(String name, Height height) {
super(name, height);
this.branch = "EEE";
}
}
class ECEStudent extends Student {
public ECEStudent(String name, Height height) {
super(name, height);
this.branch = "ECE";
}
}
class CSEStudent extends Student {
public CSEStudent(String name, Height height) {
super(name, height);
this.branch = "CSE";
}
}
class StudentAdministration {
EEEStudent e1= new EEEStudent();
Student students[];
public Student[] getStudents() {
return students;
}
public void setStudents(Student[] students) {
this.students = students;
}
public StudentAdministration(Student[] students) {
this.students = students;
}
public void sortByHeight(Student s[]) {
int n = s.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (e1.compareByHeight(s[i], s[j]) < 0) {
Student temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
}
public void displaySortedEEEStudents() {
ArrayList<Student> eee = new ArrayList<>();
for (int i = 0; i < students.length; i++) {
if (students[i].branch.equals("EEE")) {
eee.add(students[i]);
}
}
Student a[] = eee.toArray(new Student[eee.size()]);
sortByHeight(a);
int i = 0;
synchronized (this) {
while (i < a.length) {
System.out.println((i + 1) + ":" + a[i++]);
notifyAll();
try {
wait();
} catch (InterruptedException e) {
}
}
}
}
public void displaySortedECEStudents() {
ArrayList<Student> ece = new ArrayList<>();
for (int i = 0; i < students.length; i++) {
if (students[i].branch.equals("ECE")) {
ece.add(students[i]);
}
}
Student a[] = ece.toArray(new Student[ece.size()]);
sortByHeight(a);
int i = 0;
synchronized (this) {
while (i < a.length) {
System.out.println((i + 1) + ":" + a[i++]);
notifyAll();
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
}
}
}
public void displaySortedCSEStudents() {
ArrayList<Student> cse = new ArrayList<>();
for (int i = 0; i < students.length; i++) {
if (students[i].branch.equals("CSE")) {
cse.add(students[i]);
}
}
Student a[] = cse.toArray(new Student[cse.size()]);
sortByHeight(a);
int i = 0;
synchronized (this) {
while (i < a.length) {
System.out.println((i + 1) + ":" + a[i++]);
notifyAll();
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
}
}
}
}
public class UniversityDriver {
public static void main(String[] args) {
EEEStudent s1 = new EEEStudent("Divika", new Height(5, 3));
EEEStudent s2 = new EEEStudent("Anmol", new Height(6, 0));
CSEStudent s3 = new CSEStudent("Arnav", new Height(5, 9));
CSEStudent s4 = new CSEStudent("Reena", new Height(5, 1));
ECEStudent s5 = new ECEStudent("Lakshay", new Height(5, 8));
ECEStudent s6 = new ECEStudent("ram", new Height(6, 1));
Student student[] = new Student[] { s1, s2, s3, s4, s5, s6 };
StudentAdministration admin = new StudentAdministration(student);
Thread cseDept = new Thread() {
public void run() {
admin.displaySortedCSEStudents();
}
};
Thread eceDept = new Thread() {
public void run() {
admin.displaySortedECEStudents();
}
};
Thread eeeDept = new Thread() {
public void run() {
admin.displaySortedEEEStudents();
}
};
cseDept.start();
eceDept.start();
eeeDept.start();
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Height{
int feet;
int inches;
public Height(int feet, int inches) {
this.feet = feet;
this.inches = inches;
}
@Override
public String toString() {
return "Height [feet=" + feet + ", inches=" + inches + "]";
}
}
abstract class Student{
String name;
Height height;
String branch;
}
class EEEStudent extends Student implements Comparator<EEEStudent>{
EEEStudent(){
}
EEEStudent(String name,Height height,String branch){
this.name=name;
this.height=height;
this.branch=branch;
}
@Override
public String toString() {
return "EEEStudent [name=" + name + ", height=" + height + ", branch=" + branch + "]";
}
@Override
public int compare(EEEStudent o1, EEEStudent o2) {
return o2.height.feet-o1.height.feet;
}
}
class ECEStudent extends Student implements Comparator<ECEStudent>{
ECEStudent(){
}
ECEStudent(String name,Height height,String branch){
this.name=name;
this.height=height;
this.branch=branch;
}
@Override
public String toString() {
return "ECEStudent [name=" + name + ", height=" + height + ", branch=" + branch + "]";
}
@Override
public int compare(ECEStudent o1, ECEStudent o2) {
return o2.height.feet-o1.height.feet;
}
}
class CSEStudent extends Student implements Comparator<CSEStudent>{
CSEStudent(){
}
CSEStudent(String name,Height height,String branch){
this.name=name;
this.height=height;
this.branch=branch;
}
@Override
public String toString() {
return "CSEStudent [name=" + name + ", height=" + height + ", branch=" + branch + "]";
}
public int compare(CSEStudent o1, CSEStudent o2) {
return o2.height.feet-o1.height.feet;
}
}
public class Multithreading {
public static void main(String[] args) {
ArrayList<Student> s= new ArrayList<>();
s.add(new EEEStudent("Shivani",new Height(5,2),"EEE"));
s.add(new CSEStudent("Rani",new Height(4,1),"CSE"));
s.add(new ECEStudent("Puja",new Height(4,2),"ECE"));
s.add(new EEEStudent("Vishali",new Height(4,6),"EEE"));
s.add(new CSEStudent("Punita",new Height(4,2),"CSE"));
s.add(new ECEStudent("Rojee",new Height(5,6),"ECE"));
ArrayList<CSEStudent>cse= new ArrayList<>();
ArrayList<EEEStudent>eee= new ArrayList<>();
ArrayList<ECEStudent>ece= new ArrayList<>();
for(int i=0;i<s.size();i++) {
Student s1 =s.get(i);
if(s1.branch.equals("CSE")) {
cse.add((CSEStudent) s1);
}
}
for(int i=0;i<s.size();i++) {
Student s2= s.get(i);
if(s2.branch.equals("EEE")) {
eee.add((EEEStudent) s2);
}
}
for(int i =0;i<s.size();i++) {
Student s3= s.get(i);
if(s3.branch.equals("ECE")) {
ece.add((ECEStudent) s3);
}
}
Collections.sort(cse,new CSEStudent());
Collections.sort(eee,new EEEStudent());
Collections.sort(ece,new ECEStudent());
new Thread() {
public void run() {
try {
Multithreading student = new Multithreading();
student.printCSE(cse);
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
}.start();
System.out.println("---------------------------------------------------------");
new Thread() {
public void run() {
try {
Multithreading student = new Multithreading();
student.printECE(ece);
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
}.start();
System.out.println("----------------------------------------------------------");
new Thread() {
public void run() {
try {
Multithreading student = new Multithreading();
student.printEEE(eee);
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
}.start();
System.out.println("----------------------------------------------------------");
}
public synchronized void printCSE(List<CSEStudent> cse) throws InterruptedException{
int i=0;
boolean flag=false;
while(i<cse.size()) {
while(flag) {
wait();
}
flag=true;
System.out.println(cse.get(i++));
flag=false;
notifyAll();
}
}
public synchronized void printECE(List<ECEStudent> ece) throws InterruptedException{
int i=0;
boolean flag=false;
while(i<ece.size()) {
while(flag) {
wait();
}
flag=true;
System.out.println(ece.get(i++));
flag=false;
notifyAll();
}
}
public synchronized void printEEE(List<EEEStudent> eee) throws InterruptedException{
int i=0;
boolean flag=false;
while(i<eee.size()) {
while(flag) {
wait();
}
flag=true;
System.out.println(eee.get(i++));
flag=false;
notifyAll();
}
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Height{
int feet;
int inches;
public Height(int feet, int inches) {
this.feet = feet;
this.inches = inches;
}
@Override
public String toString() {
return "Height [feet=" + feet + ", inches=" + inches + "]";
}
}
abstract class Student{
String name;
Height height;
String branch;
}
class EEEStudent extends Student implements Comparator<EEEStudent>{
EEEStudent(){
}
EEEStudent(String name,Height height,String branch){
this.name=name;
this.height=height;
this.branch=branch;
}
@Override
public String toString() {
return "EEEStudent [name=" + name + ", height=" + height + ", branch=" + branch + "]";
}
@Override
public int compare(EEEStudent o1, EEEStudent o2) {
return o2.height.feet-o1.height.feet;
}
}
class ECEStudent extends Student implements Comparator<ECEStudent>{
ECEStudent(){
}
ECEStudent(String name,Height height,String branch){
this.name=name;
this.height=height;
this.branch=branch;
}
@Override
public String toString() {
return "ECEStudent [name=" + name + ", height=" + height + ", branch=" + branch + "]";
}
@Override
public int compare(ECEStudent o1, ECEStudent o2) {
return o2.height.feet-o1.height.feet;
}
}
class CSEStudent extends Student implements Comparator<CSEStudent>{
CSEStudent(){
}
CSEStudent(String name,Height height,String branch){
this.name=name;
this.height=height;
this.branch=branch;
}
@Override
public String toString() {
return "CSEStudent [name=" + name + ", height=" + height + ", branch=" + branch + "]";
}
public int compare(CSEStudent o1, CSEStudent o2) {
return o2.height.feet-o1.height.feet;
}
}
public class Multithreading {
public static void main(String[] args) {
ArrayList<Student> s= new ArrayList<>();
s.add(new EEEStudent("Shivani",new Height(5,2),"EEE"));
s.add(new CSEStudent("Rani",new Height(4,1),"CSE"));
s.add(new ECEStudent("Puja",new Height(4,2),"ECE"));
s.add(new EEEStudent("Vishali",new Height(4,6),"EEE"));
s.add(new CSEStudent("Punita",new Height(4,2),"CSE"));
s.add(new ECEStudent("Rojee",new Height(5,6),"ECE"));
ArrayList<CSEStudent>cse= new ArrayList<>();
ArrayList<EEEStudent>eee= new ArrayList<>();
ArrayList<ECEStudent>ece= new ArrayList<>();
for(int i=0;i<s.size();i++) {
Student s1 =s.get(i);
if(s1.branch.equals("CSE")) {
cse.add((CSEStudent) s1);
}
}
for(int i=0;i<s.size();i++) {
Student s2= s.get(i);
if(s2.branch.equals("EEE")) {
eee.add((EEEStudent) s2);
}
}
for(int i =0;i<s.size();i++) {
Student s3= s.get(i);
if(s3.branch.equals("ECE")) {
ece.add((ECEStudent) s3);
}
}
Collections.sort(cse,new CSEStudent());
Collections.sort(eee,new EEEStudent());
Collections.sort(ece,new ECEStudent());
new Thread() {
public void run() {
try {
Multithreading student = new Multithreading();
student.printCSE(cse);
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
}.start();
System.out.println("---------------------------------------------------------");
new Thread() {
public void run() {
try {
Multithreading student = new Multithreading();
student.printECE(ece);
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
}.start();
System.out.println("----------------------------------------------------------");
new Thread() {
public void run() {
try {
Multithreading student = new Multithreading();
student.printEEE(eee);
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
}.start();
System.out.println("----------------------------------------------------------");
}
public synchronized void printCSE(List<CSEStudent> cse) throws InterruptedException{
int i=0;
boolean flag=false;
while(i<cse.size()) {
while(flag) {
wait();
}
flag=true;
System.out.println(cse.get(i++));
flag=false;
notifyAll();
}
}
public synchronized void printECE(List<ECEStudent> ece) throws InterruptedException{
int i=0;
boolean flag=false;
while(i<ece.size()) {
while(flag) {
wait();
}
flag=true;
System.out.println(ece.get(i++));
flag=false;
notifyAll();
}
}
public synchronized void printEEE(List<EEEStudent> eee) throws InterruptedException{
int i=0;
boolean flag=false;
while(i<eee.size()) {
while(flag) {
wait();
}
flag=true;
System.out.println(eee.get(i++));
flag=false;
notifyAll();
}
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Declare an abstract class Student with the following attributes:
The class also has the following behaviors which return -1, 0, or 1 based on the result of comparison with another student:
Extend the abstract class into the following 3 concrete classes and provide the parameterized constructors accordingly:
Create a service class called StudentAdministration which helps the university administrators see details of each department.
This class has the following data member with its getters and setters:
The class has the following methods:
-- public void displaySortedEEEStudents(): you must create eeeStudents array from this.students array
-- public void displaySortedECEStudents(): you must create eceStudents array from this.students array
-- public void displaySortedCSEStudents(): you must create cseStudents array from this.students array
In the UniversityDriver class:
Hint:
Beta Was this translation helpful? Give feedback.
All reactions