lap os & oop so good for computer science, Assignments of Computer science

for more don't forget to contact me

Typology: Assignments

2020/2021

Uploaded on 02/20/2023

puthsabbos-ngang
puthsabbos-ngang 🇰🇭

4 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Exercise1:
package Exercise1;
public class Cat {
private String name;
public String color;
public void setName(String _name) {
name = _name;
}
public String getName(){
return name;
}
public void cry(){
System.out.println(name+" is crying Meow! Meow!");
}
}
package Exercise1;
public class MainForCat {
public static void main(String[] args){
Cat c = new Cat();
c.setName( "chenny ");
c.color = "brewn";
c.cry();
System.out.println(c.getName()+"'s color is "+c.color);
}
}
Exercise2:
package Exercise2;
public class Dog {
public String name;
public Dog(){
}
public Dog(String _name){
name = _name;
}
public void cry(){
System.out.println(name+" is crying wooh! wooh!");
}
public static void main(String[] args){
Dog c = new Dog("kiki ");
c.cry();
}
}
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download lap os & oop so good for computer science and more Assignments Computer science in PDF only on Docsity!

Exercise1:

package Exercise1; public class Cat { private String name; public String color; public void setName(String _name) { name = _name; } public String getName(){ return name; } public void cry(){ System. out .println(name+" is crying Meow! Meow!"); } } package Exercise1; public class MainForCat { public static void main(String[] args){ Cat c = new Cat(); c.setName( "chenny "); c.color = "brewn"; c.cry(); System. out .println(c.getName()+"'s color is "+c.color); } }

Exercise2:

package Exercise2; public class Dog { public String name; public Dog(){ } public Dog(String _name){ name = _name; } public void cry(){ System. out .println(name+" is crying wooh! wooh!"); } public static void main(String[] args){ Dog c = new Dog("kiki "); c.cry(); }

Exercise3:

package Exercise3; public class Book { public String title,author,isbn; public Book(){} public Book(String title){this.title=title;} public Book(String title, String author, String isbn){ this.title = title; this.author = author; this.isbn = isbn; } public void setTitle(String title){this.title=title;} public void setAuthor(String author){this.author=author;} public void setIsbn(String isbn){this.isbn=isbn;} public String getTitle(){return title;} public String getAuthor(){return author;} public String getIsbn(){return isbn;} public static void main(String[] args){ Book b = new Book(); b.setTitle("Java Book"); b.setAuthor("Java Author"); b.setIsbn("325-1273995305"); b = new Book("Java Book"); b = new Book("Java Book","Java Author","325-1273995305"); System. out .println("Title: "+b.getTitle()); System. out .println("Author: "+b.getAuthor()); System. out .println("ISBN: "+b.getIsbn()); }

Exercise5:

package Exercise5; public class Student { public String firstname,lastname,student_id,address; public Student(){ } public Student(String firstname,String lastname,String student_id){ this.firstname = firstname; this.lastname = lastname; this.student_id = student_id; } public Student(String firstname,String lastname,String student_id,String address){ this.firstname = firstname; this.lastname = lastname; this.student_id = student_id; this.address = address; } public void setFirstname(String firstname){this.firstname=firstname;} public void setLastname(String lastname){this.lastname=lastname;} public void setStudent_id(String student_id){this.student_id=student_id;} public void setAddress(String address){this.address=address;} public String getFirstname(){return firstname;} public String getLastname(){return lastname;} public String getStudent_id(){return student_id;} public String getAddress(){return address;} public void printInfo(){ System. out .println("Name: "+getFirstname()+" "+getLastname()); System. out .println("ID: "+getStudent_id()); System. out .println("Address: "+getAddress()); } public static void main(String[] args){ System. out .println("========== Student1 =========="); Student s1 = new Student(); s1.setFirstname("Ngang"); s1.setLastname("Puthsabbos"); s1.setStudent_id("IDTB080142"); s1.setAddress("Takeo"); s1.printInfo(); System. out .println("========== Student2 =========="); Student s2 = new Student("Ngoun","Ratanakvichet","IDTB080160"); s2.setAddress("Phnom Penh"); s2.printInfo(); System. out .println("========== Student3 =========="); Student s3 = new Student("Hoy","Seiha","IDTB080146","Seam Reab"); s3.printInfo(); }

Exercise6:

package Exercise6; import java.security.PublicKey; public class Book { public String ISBN,title,author; public int publishYear; public Book(){} public Book(String title){this.title = title;} public Book(String title, String author){ this.title=title; this.author=author; } public Book(String ISBN,String title,String author){ this.ISBN=ISBN; this.title=title; this.author=author; } public Book(String ISBN,String title,String author,int publishYear){ this.ISBN=ISBN; this.title=title; this.author=author; this.publishYear=publishYear; } public void setISBN(String ISBN){this.ISBN=ISBN;} public void setTitle(String title){this.title=title;} public void setAuthor(String author){this.author=author;} public void setPublishYear(intpublishYear){ this.publishYear=publishYear;} public String getISBN(){return ISBN;} public String getTitle(){return title;} public String getAuthor(){return author;} public int getPublishYear(){return publishYear;} public void printInfo(){ System. out .println("Title: "+getTitle()); System. out .println("Author: "+getAuthor()); System. out .println("Publish Year: "+getPublishYear()); System. out .println("ISBN: "+getISBN()); }

public static void sortByTitle(Book[] books){ for(int i= 0 ; i<books.length; i++){ for(int j=i+ 1 ; j<books.length; j++){ if(books[i].getTitle().compareTo(books[j].getTitle())> 0 ){ Book tmp = books[i]; books[i]=books[j]; books[j]=tmp; } } } for(int i= 0 ; i<books.length; i++){ System. out .println("Book"+(i+ 1 )); books[i].printInfo(); } } public static void main(String[] args){ Book [] books = new Book[ 5 ]; while (true){ System. out .println("1, Fill data."); System. out .println("2, Display all Books."); System. out .println("3, Search book by title."); System. out .println("4, Search book by ISBN."); System. out .println("5, Sort book by ISBN."); System. out .println("6, Sort book by title."); System. out .println("7, Exit."); System. out .print("Enter your option: "); int opt = sc .nextInt(); sc .nextLine(); if(opt>= 1 && opt<= 6 ){ switch (opt){ case 1 - > fillData (books); case 2 - > printAll (books); case 3 - >{ System. out .print("Enter a title: "); int index= searchBookByTitle (books, sc .nextLine()); if(index>= 0 ){ System. out .println("Found!"); books[index].printInfo(); }else System. out .println("Not Found!"); } case 4 - >{ System. out .print("Enter an ISBN: "); int index= searchBookByISBN (books, sc .nextLine()); if(index>= 0 ){ System. out .println("Found!"); books[index].printInfo(); }else System. out .println("Not Found!"); } case 5 - > sortByISBN (books); case 6 - > sortByTitle (books); } } else if (opt== 7 ) break; else System. out .println("Wrong Option!"); System. out .println(); } }

Exercise 7 :

package Exercise7; public class Student { public String id,name,department,generation; public Student(){} public Student(String name){this.name=name;} public Student(String id,String name){ this.id=id; this.name=name; } public Student(String id,String name,String department){ this.id=id; this.name=name; this.department=department; } public Student(String id,String name,String department,String generation){ this.id=id; this.name=name; this.department=department; this.generation=generation; } public void setId(String id){this.id=id;} public void setName(String name){this.name=name;} public void setDepartment(String department){this.department=department;} public void setGeneration(String generation){this.generation=generation;} public String getId(){return id;} public String getName(){return name;} public String getDepartment(){return department;} public String getGeneration(){return generation;} public void printInfo(){ System. out .println("ID: "+getId()); System. out .println("Name: "+getName()); System. out .println("Department: "+getDepartment()); System. out .println("Generation: "+getGeneration()); }

for(int i= 0 ; i<students.length; i++){ for(int j=i+ 1 ; j<students.length; j++){ if(students[i].getId().compareTo(students[j].getId())> 0 ){ Student tmp = students[i]; students[i]=students[j]; students[j]=tmp; } } } for(int i= 0 ; i<students.length; i++){ System. out .println("Student"+(i+ 1 )); students[i].printInfo(); } } public static void main(String[] args){ Student[] students = new Student[ 10 ]; while (true){ System. out .println("1, Fill data."); System. out .println("2, Display all Student."); System. out .println("3, Search Student by name."); System. out .println("4, Search Student by ID."); System. out .println("5, Sort Student by name."); System. out .println("6, Sort Student by ID."); System. out .println("7, Exit."); System. out .print("Enter your option: "); int opt = sc .nextInt(); sc .nextLine(); if(opt>= 1 && opt<= 6 ){ switch (opt){ case 1 - > fillData (students); case 2 - > printAll (students); case 3 - >{ System. out .print("Enter a Name: "); int index= searchStudentByName (students, sc .nextLine()); if(index>= 0 ){ System. out .println("Found!"); students[index].printInfo(); }else System. out .println("Not Found!"); } case 4 - >{ System. out .print("Enter an ID: "); int index= searchStudentById (students, sc .nextLine()); if(index>= 0 ){ System. out .println("Found!"); students[index].printInfo(); }else System. out .println("Not Found!"); } case 5 - > sortByName (students); case 6 - > sortById (students); } } else if (opt== 7 ) break; else System. out .println("Wrong Option!"); System. out .println(); } }