/*------------------------------------------------------ My name: My student number: My course code: CSIT121 My email address: Assignment number: 1 -------------------------------------------------------*/ import java.util.Scanner; public class Enrolment { public static void main(String[] args) { // TODO Auto-generated method stub //Adding all the subjects Subject CSIT111 = new Subject("CSIT111","Programming Fundamentals",6); Subject CSIT113 = new Subject("CSIT113","Problem Solving",6); Subject CSIT114 = new Subject("CSIT114","System Analysis",6); Subject CSIT115 = new Subject("CSIT115","Data Management and Security",6); Subject CSIT121 = new Subject("CSIT121","Object Oriented Design and Programming",6); Subject CSIT127 = new Subject("CSIT127","Networks and Communications",6); Subject CSIT128 = new Subject("CSIT128","Introduction to Web Technology",6); Subject CSCI235 = new Subject("CSCI235","Database Systems",6); Subject CSCI251 = new Subject("CSCI251","Advanced Programming",6); Subject CSIT214 = new Subject("CSIT214","IT Project Management",6); Subject MATH221 = new Subject("MATH221","Mathematics for Computer Science",6); Subject CSCI203 = new Subject("CSCI203","Algorithms and Data Structures",6); Subject CSIT226 = new Subject("CSIT226","Human Computer Interaction",6); Subject CSIT314 = new Subject("CSIT314","Software development Methodologies",6); Subject CSIT321 = new Subject("CSIT321","Project",12); Subject CSCI317 = new Subject("CSCI317","Database Performance Tuning",6); Subject INFO411 = new Subject("INFO411","Data Mining and Knowledge Discovery",6); Subject CSCI316 = new Subject("CSCI316","Big Data Mining Techniques and Implementation",6); Subject ISIT312 = new Subject("ISIT312","Big Data Management",6); Subject CSCI301 = new Subject("CSCI301","Contemporary Topics in Security",6); Subject CSCI262 = new Subject("CSCI262","System Security",6); Subject CSCI369 = new Subject("CSCI369","Ethical Hacking",6); Subject CSIT302 = new Subject("CSIT302","Cyber Security",6); Subject CSCI361 = new Subject("CSCI361","Cryptography and Secure Applications",6); Subject CSCI368 = new Subject("CSCI368","Network Security",6); Subject CSCI376 = new Subject("CSCI376","Multicore and GPU Programming",6); Subject CSCI236 = new Subject("CSCI236","3D Modelling and Animation",6); Subject CSCI336 = new Subject("CSCI336","Interactive Computer Graphics",6); Subject CSCI366 = new Subject("CSCI366","Mobile Multimedia",6); Subject CSCI356 = new Subject("CSCI356","Game Engine Essentials",6); Subject CSCI334 = new Subject("CSCI334","Software Design",6); Subject ISIT219 = new Subject("ISIT219","Knowledge and Information Engineering",6); Subject CSCI318 = new Subject("CSCI318","Software Engineering Practices and Principles",6); Subject ISIT315 = new Subject("ISIT315","Semantic Web",6); //Adding all the Majors //Big Data Major bigData = new Major("Big Data"); Subject[] bDataCores = {CSCI317, INFO411,CSCI316,ISIT312}; bigData.addMCores(bDataCores); //Cyber Security Major cyberSec = new Major("Cyber Security"); Subject[] cyberSecCores = {CSCI301, CSCI262, CSCI369, CSIT302}; cyberSec.addMCores(cyberSecCores); //Digital System Security Major digitalSysSec = new Major("Digital System Security"); Subject[] digitalSysSecCores = {CSCI361, CSCI262, CSCI368, CSCI376}; digitalSysSec.addMCores(digitalSysSecCores); //Game and Mobile Development Major gameMobDev = new Major("Game and Mobile Development"); Subject[] gameMobDevCores = {CSCI236, CSCI336, CSCI366, CSCI356,CSCI376}; gameMobDev.addMCores(gameMobDevCores); //Software Engineering Major softEng = new Major("Software Engineering"); Subject[] softEngCores = {CSCI334, ISIT219, CSCI318, ISIT315}; softEng.addMCores(softEngCores); //List Of Major Course Major[] BCSMajors = {bigData, cyberSec, digitalSysSec, gameMobDev, softEng}; //List of Core Course Subject[] cCores = {CSIT111, CSIT113, CSIT114, CSIT115, CSIT121, CSIT127, CSIT128,CSCI235, CSCI251, CSIT214, MATH221, CSCI203, CSIT226, CSIT314, CSIT321 }; //List of Elective Course Subject[] cEles = {CSCI317, INFO411, CSCI316, ISIT312, CSCI301, CSCI262, CSCI369,CSIT302, CSCI361, CSCI368, CSCI376, CSCI236, CSCI336, CSCI366, CSCI356, CSCI334, ISIT219, CSCI318, ISIT315}; //Adding Course Course bcs = new Course("Bachelor of Computer Science"); //Adding Core, Major, and Elective subjects to the Bachelor of Computer Science bcs.addCores(cCores); bcs.addMajors(BCSMajors); bcs.addElectives(cEles); //Display Module System.out.println("Welcome to enrol the Bachelor of Computer Science course."); System.out.println("The course structure is as follows : "); System.out.println("------------------------"); System.out.println(bcs); System.out.println(); //Print Core Subjects System.out.println("Core Subjects :"); for(Subject core: cCores) { System.out.println(core); } //Print Major Courses System.out.println(); for(Major major: BCSMajors) { System.out.println(major.getName()+" Major"); major.Display(); System.out.println(); } System.out.println(); //Print Electives System.out.println("Elective Subjects :"); for(Subject elective:cEles) { System.out.println(elective); } System.out.println("-----------------"); System.out.println(); System.out.println(); //Personal Details Scanner sc = new Scanner(System.in); //New Student Object Student S1 = new Student(); int total_credit = 96; //96 Credit points are available as Core Course System.out.println("Your following personal information are required to complete the enrolment."); //Set Student Name System.out.println("Please input your full name : "); String name = sc.nextLine(); S1.setName(name); System.out.println(); //Set Student Number System.out.println("Please input your student number : "); String number_input = sc.nextLine(); int number = Integer.parseInt(number_input); S1.setNumber(number); System.out.println(); //Set Student Gender System.out.println("Please input your gender: "); String gender = sc.nextLine(); S1.setGender(gender); System.out.println(); //Set Student Date Of Birth System.out.println("Please input your date of birth (dd/mm/yyyy) :"); String dob = sc.nextLine(); S1.setDob(dob); System.out.println(); //Choosing Major System.out.println("Thanks for your information."); System.out.println("Inorder to complete the enrolment, please select a major from the list."); System.out.println("1: Big Data"); System.out.println("2: Cyber Security"); System.out.println("3: Digital System Security"); System.out.println("4: Game and Mobile Development"); System.out.println("5: Software Engineering"); System.out.println(); System.out.print("Please input the index number before the major: "); String index_input = sc.nextLine(); int index = Integer.parseInt(index_input); Major major_choice = null;//Assigning the major chosen by the student to a variable System.out.println(); System.out.println("You enrolled into: "); // All the subjects are of 6 credits int subject_credit = 6; switch(index) { case 1: { major_choice = bigData; System.out.println(major_choice.getName()+" Major"); bigData.Display(); total_credit += 4*subject_credit; //4 courses are available in the Major Big Data break; } case 2: { major_choice = cyberSec; System.out.println(major_choice.getName()+" Major"); cyberSec.Display(); total_credit += 4*subject_credit; //4 courses are available in the Major Cyber Security break; } case 3: { major_choice = digitalSysSec; System.out.println(major_choice.getName()+" Major"); digitalSysSec.Display(); total_credit += 4*subject_credit; //4 courses are available in the Major Digital System Security break; } case 4: { major_choice = gameMobDev; System.out.println(major_choice.getName()+" Major"); gameMobDev.Display(); total_credit += 5*subject_credit; //5 courses are available in the Major Game and Mobile Development break; } case 5: { major_choice = softEng; System.out.println(major_choice.getName()+" Major"); softEng.Display(); total_credit += 4*subject_credit; //4 courses are available in the Major Software Engineering break; } default:System.out.println("Wrong Input"); } System.out.println(); //Calculating Electives Required int remaining_credit = bcs.getCredit() - total_credit;//Remaining Credit points required to reach the minimum target int elective_count = remaining_credit/subject_credit ; //Number of electives required to reach the minimum //Choosing Electives System.out.println("Inorder to complete the enrolment, please select the elective subjects from the list. "); System.out.println(); System.out.println("Elective Subjects"); for(Subject elective:cEles) { System.out.println(elective); } System.out.println(); //Available Electives Subject[] cEles_available = new Subject[cEles.length]; Subject[] major_Subjects = major_choice.getMCore();//Subjects in the selected Major for(int i=0;i<cEles.length;i++) { int flag = 0; // To keep count for(Subject major:major_Subjects) { if(!(cEles[i].getCode().equals(major.getCode()))) { flag++; } } if(flag==major_Subjects.length) { cEles_available[i] = cEles[i]; }else { cEles_available[i] = new Subject("null","null",0);//Adding a blank Subject to the available electives list, if flag is not equal to number of subjects in the major selected } } //Adding Electives chosen by the student Subject[] elective_array = new Subject[elective_count+1]; int elective_index = elective_count; //index of the elective array initialized to the number of electives required int elective_selected = 0;//Total number of electives selected by the student //while loop starts to check if the minimum number of electives is selected while(elective_count>0) { System.out.println("Please select "+elective_count+" more elective subjects: "); String elective_choice = sc.nextLine(); if(elective_choice.length()==7) { //Only One Elective is selected at a time for(Subject elective:cEles_available) { if(elective_choice.equals(elective.getCode())){ elective_array[elective_index - elective_count] = elective; elective_count--; //Decrementing elective count total_credit += subject_credit; //Incrementing credit points earned elective_selected++; }else { continue; } } }else if(elective_choice.length()==16){ //When two electives are selected with a comma and space String elective_choice_1 = elective_choice.substring(0, 7); //Taking the subject codes separately String elective_choice_2 = elective_choice.substring(9, 16); for(Subject elective:cEles_available) { if(elective_choice_1.equals(elective.getCode())&&!(elective_choice_2.equals(elective.getCode()))) { elective_array[elective_index - elective_count] = elective; elective_count--; total_credit += subject_credit; elective_selected++; }else if(elective_choice_2.equals(elective.getCode())&&!(elective_choice_1.equals(elective.getCode()))){ elective_array[elective_index - elective_count] = elective; elective_count--; total_credit += subject_credit; elective_selected++; }else if(elective_choice_1.equals(elective.getCode())&&elective_choice_2.equals(elective.getCode())){ elective_array[elective_index - elective_count] = elective; elective_count--; elective_array[elective_index-elective_count] = elective; elective_count--; total_credit += subject_credit; elective_selected++; }else { continue; } } }else { break; } } System.out.println(); System.out.println(); //Printing the student Details System.out.println("Congratulations. You had completed the enrolment to Bachelor of Computer Science course. "); System.out.println("--------------------"); System.out.println(S1); System.out.println(); //Print Core Subjects System.out.println("Cores"); for(Subject core: cCores) { System.out.println(core); } System.out.println(); //Print Major Courses System.out.println("Major : "+major_choice.getName()); major_choice.Display(); System.out.println(); //Print Electives System.out.println("Electives :"); for(int i=0;i<elective_selected;i++) { System.out.println(elective_array[i]); } System.out.println("-----------------"); System.out.println(); System.out.println("Total Enrolled Credit Points : "+total_credit+"pt"); sc.close(); } } //Student Class class Student { //Fields private String name; private int number; private String gender; private String dob; //Public setter method for name public void setName(String name) { this.name = name; } //Public setter method for number public void setNumber(int number) { this.number = number; } //public setter method for gender public void setGender(String gender) { this.gender = gender; } //public setter method for Date of Birth public void setDob(String dob) { this.dob = dob; } //Print Method @Override public String toString() { return "Student : "+name+" ("+number+", "+gender+", "+dob+" )"; } } //Subject Class class Subject { ////Fields private String name; private String code; private int credit; //Constructor method public Subject(String code, String name, int credit) { this.code = code; this.name = name; this.credit = credit; } //public getter method for name public String getName() { return name; } //public setter method for name public void setName(String name) { this.name = name; } //public getter method for code public String getCode() { return code; } @Override public String toString() { return code+" ("+name+", "+credit+"pt)"; } } //Major Class class Major { ////Fields private String name; private Subject[] MCore; //Constructor method public Major(String name) { this.name = name; } //public setter method for Major Courses public void addMCores(Subject[] MCore) { this.MCore = MCore; } //public getter method for Major courses public Subject[] getMCore() { return MCore; } //public getter method for name public String getName() { return name; } //public method for displaying various courses public void Display() { for(Subject subject:MCore) { System.out.println(subject); } } } //Course Class class Course { ////Fields private String name; private int credit_min = 144; private Major[] major; private Subject[] core; private Subject[] elective; //Constructor method public Course(String name) { this.name = name; } //Public method for adding Core Subjects public void addCores(Subject[] core) { this.core = core; } //Public method for adding Major public void addMajors(Major[] major) { this.major = major; } //Public method for adding elective courses public void addElectives(Subject[] elective) { this.elective = elective; } //Public method to get minimum credit points required public int getCredit() { return credit_min; } //Public method to print by overriding toString() @Override public String toString() { return "Course : "+name; } }
Write, Run & Share Java code online using OneCompiler's Java online compiler for free. It's one of the robust, feature-rich online compilers for Java language, running the Java LTS version 17. Getting started with the OneCompiler's Java editor is easy and fast. The editor shows sample boilerplate code when you choose language as Java and start coding.
OneCompiler's Java online editor supports stdin and users can give inputs to the programs using the STDIN textbox under the I/O tab. Using Scanner class in Java program, you can read the inputs. Following is a sample program that shows reading STDIN ( A string in this case ).
import java.util.Scanner;
class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your name: ");
String inp = input.next();
System.out.println("Hello, " + inp);
}
}
OneCompiler supports Gradle for dependency management. Users can add dependencies in the build.gradle
file and use them in their programs. When you add the dependencies for the first time, the first run might be a little slow as we download the dependencies, but the subsequent runs will be faster. Following sample Gradle configuration shows how to add dependencies
apply plugin:'application'
mainClassName = 'HelloWorld'
run { standardInput = System.in }
sourceSets { main { java { srcDir './' } } }
repositories {
jcenter()
}
dependencies {
// add dependencies here as below
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
}
Java is a very popular general-purpose programming language, it is class-based and object-oriented. Java was developed by James Gosling at Sun Microsystems ( later acquired by Oracle) the initial release of Java was in 1995. Java 17 is the latest long-term supported version (LTS). As of today, Java is the world's number one server programming language with a 12 million developer community, 5 million students studying worldwide and it's #1 choice for the cloud development.
short x = 999; // -32768 to 32767
int x = 99999; // -2147483648 to 2147483647
long x = 99999999999L; // -9223372036854775808 to 9223372036854775807
float x = 1.2;
double x = 99.99d;
byte x = 99; // -128 to 127
char x = 'A';
boolean x = true;
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
// code
} else {
// code
}
Example:
int i = 10;
if(i % 2 == 0) {
System.out.println("i is even number");
} else {
System.out.println("i is odd number");
}
Switch is an alternative to If-Else-If ladder and to select one among many blocks of code.
switch(<conditional-expression>) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
//code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition. Usually for loop is preferred when number of iterations is known in advance.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(<condition>){
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (<condition>);
Class is the blueprint of an object, which is also referred as user-defined data type with variables and functions. Object is a basic unit in OOP, and is an instance of the class.
class
keyword is required to create a class.
class Mobile {
public: // access specifier which specifies that accessibility of class members
string name; // string variable (attribute)
int price; // int variable (attribute)
};
Mobile m1 = new Mobile();
public class Greeting {
static void hello() {
System.out.println("Hello.. Happy learning!");
}
public static void main(String[] args) {
hello();
}
}
Collection is a group of objects which can be represented as a single unit. Collections are introduced to bring a unified common interface to all the objects.
Collection Framework was introduced since JDK 1.2 which is used to represent and manage Collections and it contains:
This framework also defines map interfaces and several classes in addition to Collections.
Collection | Description |
---|---|
Set | Set is a collection of elements which can not contain duplicate values. Set is implemented in HashSets, LinkedHashSets, TreeSet etc |
List | List is a ordered collection of elements which can have duplicates. Lists are classified into ArrayList, LinkedList, Vectors |
Queue | FIFO approach, while instantiating Queue interface you can either choose LinkedList or PriorityQueue. |
Deque | Deque(Double Ended Queue) is used to add or remove elements from both the ends of the Queue(both head and tail) |
Map | Map contains key-values pairs which don't have any duplicates. Map is implemented in HashMap, TreeMap etc. |