From 4b801ac72431009b533f93bd1f0b9bb88c34f02d Mon Sep 17 00:00:00 2001 From: Swii2313 Date: Thu, 11 Sep 2025 19:26:48 +0200 Subject: [PATCH] Completed LAB1 challenges --- .idea/.gitignore | 3 + .idea/lab-java-basics.iml | 11 ++++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ .../lab-java-basics/Challenge1.class | Bin 0 -> 1804 bytes .../lab-java-basics/Challenge4.class | Bin 0 -> 255 bytes out/production/lab-java-basics/Employee.class | Bin 0 -> 2133 bytes out/production/lab-java-basics/Intern.class | Bin 0 -> 1020 bytes .../lab-java-basics/challenge2.class | Bin 0 -> 1511 bytes out/production/lab-java-basics/main.class | Bin 0 -> 1379 bytes out/production/lab-java-basics/main1.class | Bin 0 -> 789 bytes out/production/lab-java-basics/main3.class | Bin 0 -> 1391 bytes source/Challenge1.java | 37 +++++++++++ source/Challenge4.java | 2 + source/Employee.java | 60 ++++++++++++++++++ source/Intern.java | 35 ++++++++++ source/challenge2.java | 45 +++++++++++++ source/main.java | 17 +++++ source/main1.java | 11 ++++ source/main3.java | 25 ++++++++ 21 files changed, 266 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/lab-java-basics.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 out/production/lab-java-basics/Challenge1.class create mode 100644 out/production/lab-java-basics/Challenge4.class create mode 100644 out/production/lab-java-basics/Employee.class create mode 100644 out/production/lab-java-basics/Intern.class create mode 100644 out/production/lab-java-basics/challenge2.class create mode 100644 out/production/lab-java-basics/main.class create mode 100644 out/production/lab-java-basics/main1.class create mode 100644 out/production/lab-java-basics/main3.class create mode 100644 source/Challenge1.java create mode 100644 source/Challenge4.java create mode 100644 source/Employee.java create mode 100644 source/Intern.java create mode 100644 source/challenge2.java create mode 100644 source/main.java create mode 100644 source/main1.java create mode 100644 source/main3.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/lab-java-basics.iml b/.idea/lab-java-basics.iml new file mode 100644 index 0000000..b7b3be2 --- /dev/null +++ b/.idea/lab-java-basics.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a9182a4 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..505d07d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/lab-java-basics/Challenge1.class b/out/production/lab-java-basics/Challenge1.class new file mode 100644 index 0000000000000000000000000000000000000000..8a805a9ad2773a032d101ccf672079201fa3405e GIT binary patch literal 1804 zcmaJ>%W@k<6g{nxJhD8AZ5hXQBFAI$z?N~00|a9E0a0usP##1hny z5U*g7kDyqlQpJW1SvXWkWyylS;0xHNsF>3un`{$1KMB}yHa1kCyqY5CiPe&3AaFtxoV9RPCpA@b{ZOUx4sST#9qBsjwhRKfZQ|U1He(tT zq3S9yzTe&5HGZHFfn?ENCvtDu@zj+#mN(>@%bjf5uSvHm14pm-Hj~k&Lv>d913u=@j4lXP@de+H!YGi<%PQLR*TXa_0w>$G zC2UrQ4pvCR-cLegyNpvfm&RuT;}`!+jlHI#@09XvzH3Ww-6o4UTl99}W(J?*mWeNP z$*dASF2*<;OqIW-X4*B^uA&uLi~L|);K$CY+;eQRyhM0cg(u6tztw6MUX6Ii&Px|> zY&X>b?w!eKw}e7x_%-} z80T-1V1)t(tCYQq<9V)TI1A3H{3D28X(TYmQ6B?I6gXOK2h3xEt8Oe}i97o8KLks0 zzWF=MTMyCwh;{!E+3b-W3{UOg_(JYU<_b4*kMTONgVV;j56#mK`TmOX+ZN;al5;($fh`L6o;V7+9jNv#lIfi#>7noJ8GS8<+ z7-6l_&?I6V;GHR4<{ld>BXuQKsTK92tJ{1|Ekg8Vo-u<4u5vqpYxtD&VOmvQx*m@= P@Fn*$Tz`dcATjbUi#whD literal 0 HcmV?d00001 diff --git a/out/production/lab-java-basics/Challenge4.class b/out/production/lab-java-basics/Challenge4.class new file mode 100644 index 0000000000000000000000000000000000000000..1b1a559ddfd9ff38c5b212247fc12224a80362fb GIT binary patch literal 255 zcmYLD%MQU%5Is|^Ql*VQu){((cEpBQH7tnzZQXF?YLhDeWhJrj0X|C1Z6s!rGjq;7 za^9ck3%~-Q2MaY9whsq&LU#Y{3x`R*+Gs8 z_B`K9LJ+G=uCC`q7F&^MR=RPX3cVABGW;jnDy4+G6yp-r?G=v<{<%;0x=|F$Dks literal 0 HcmV?d00001 diff --git a/out/production/lab-java-basics/Employee.class b/out/production/lab-java-basics/Employee.class new file mode 100644 index 0000000000000000000000000000000000000000..f1606554b49f0dc8d1db7e230262e66398d056b4 GIT binary patch literal 2133 zcmaJ?ZBtuC6n-{@+z=8d;jK`MY0FzeX{@cbHh{LI*eKB2AXTd$maxJtH}__8FF1Vj zU+9cKz%S1DGNLnl=nwEmIX=5LKp?kHW_I`NJ?D9zv*+yQpMQ^E11RI06k_PI5Kp5U z34u$my5Q`pN1hk>K~px?RIux9`$>Ev(0AgMtCSz)L=vmiqJqJT_S3mJnMhkJ@EEaF}wz0r>w|Y;OTb%vAqLYj-jF_ z(%p8HKe_68d;5)2XBFFa&IN89HdKWB%QpY;nwQ$k)Ph)+j#IS*#qy>ruB)2Mj`V%y zTli97sw1Z;sTN)eO#HtqFMOC6Z)1|L5fJCd=Y^}7en`yBr^3v9AvmXaak_DvS{@sN?Vg15I44OD$HU|tz2-w{s z5%>TPB7!r&T?ihI3@$|m4_pi$iws`C;wf|NRQAxX7nrX`1~1{mR`3#&)F+cLr+;?Z z`rie(&NIDX5Jp0;)6-3~PFLU~Om$$*v4|J3rinH41}t44efAHdu?8+;6^J$W1}t4+ zjr9o29CepT)>+qlbf)izpU_*FVPE}>-al|TJMkBC8obL?I!j037Asz4d);P>+-V~j zJxzYpG>_oFuxa@|i$sr2pJ=-8h^E_Ukp#Mvjxi08Cp0t1D72Xa$Cz(3hmJ|FozF5J zF@1fH_vt<+*l_dgngx`Mu&^B$Tf(}qLfRNU#uLutOy)BRshIIoGp?EODPHiqzKLJp JD}0Ty{{ULwjne=C literal 0 HcmV?d00001 diff --git a/out/production/lab-java-basics/Intern.class b/out/production/lab-java-basics/Intern.class new file mode 100644 index 0000000000000000000000000000000000000000..2d2839e0ccaecc2b0df03c7ec604e0ff66c7554d GIT binary patch literal 1020 zcmZWn+fEZv6kVq+y-`{ylxu`3qP++$f_KD7s3wiY7;74RGKcnvqtltDof7&7#vjlJ zAW+CxYwzTtEL+k(DA(PtX&-D|j8qDZIHHI}5Kmwf2_jT&S9NWHSWmfi zY02(DAe3if3`wLS7*AjVX@T)`@PnSK+5$nR?+Rpm8q+C1BdbepCEHt3ObN{UfJRf= zw&NQ6%COaebj?GRjbj=!5zHnqhk1cX5B+}(fk;OqT2zq(2tWM&fg=K2S`=9E zTQPcG7iXmEsHs%T*jVRRS}*BY#-hjyB)7M>jbRB*eQ`N~6%PwlmD$lShW%*}XGdf%x9e%h&at+RpP=0-_=?w(HnESK4kvT7B*+wCl8Zq-v(E zp7z`Os{2CjTa2V@PE%S9*)_F442ImdW{6R(Sb`06zQL0=^LWO98WDP z2rX-wf;3$iO>Y`k6g>|H2Jjdt4z?2@rKytwZgK0TX%lSPgw(1f9HL^;BCnp|6;BK^Xl97y zeSbuEm8?oxr;E0-Dlfa}zJDg_*O|etiEMSjy!-CQx#xa7e*gRA6u@I_B@jYbhh`vx zs6hIed@k39(i*I__MfSqClK8*Ez_$DgiGa495KXo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QWfYP6s%H;-R1#y~L^xV=>zd?Rej_HkB0; zv81D9r|RRJK>9Mj942wz!-W7BQ4*NgWq#ON!0WoZVMMsnU?k;cX7*kE6Wi$kmvM!` zB+52qDB~Vol^Cu|($xUh@GTKHlwFn`B~U1p2O*v5jQ}@si#Rm>h{O8K<>~4B06*Zi zz{!r>QZ*B`q#FK{^((6dg;Lh5w?;6&-=U?AltVP(8Q!-*kkft0!?kgI=eH{zre^OI|s z?YO1pwR4|!9?f+YZsMZ2T@+&(Ea&hD zD;^%Zlsyrc-0w{ty*68_(r?jz7UyNl8dtGj59V*ruPpkOhIR9p*XlBmSPQ-SjXF%6^Y;hDhc|2K8@ z+>HxoQRTbgz7&LeIXM4Fslruo9li7h;`d$k9nMY-j=P+Lz5!-&k5U#hxX%-J`8W3U z2-k;y!h4a9> zi|+tx_bHr%Pv|tp$+pP-BwbJQX}XG|wEYY}V{DgRu}=XtK8~Pu6%Q~^f<;W>C+_%K bVu6|z7I_Cm5?JDRhz6w`cdKaPIZk~53m8tA literal 0 HcmV?d00001 diff --git a/out/production/lab-java-basics/main1.class b/out/production/lab-java-basics/main1.class new file mode 100644 index 0000000000000000000000000000000000000000..75bacb1cce5696beb51c45b080f6dcb1eedf7ee2 GIT binary patch literal 789 zcmZuvYikoh6g`vIZZ<3CZLL$9(7zYuwvsfR!^Dj&Zn^)lgDsI zmTL*C3R`bTBqk{-#%&W<88(_U_{zH^ zi;x%Lfn>PosX)G(jr%g%7k!_QvKJ17-xHD2@joyVAF3(&@RrqWD%Tr^y7S>YKa>~I zkP0g2q#06*447W>m_%lE!&x+tFO}A}mJ*w~57ZKu{vAD1AnVA{+@PpME2A~Lehl_y znckpTr$JwnW;;26O*#zZaUWZR=;BZG@|x8B4&!}Mmhl_Y>PO30u_`!%Av4HFItXwiB?jBV^tRKf}#;33H}Jfb*-$A1BUp`*G0 literal 0 HcmV?d00001 diff --git a/out/production/lab-java-basics/main3.class b/out/production/lab-java-basics/main3.class new file mode 100644 index 0000000000000000000000000000000000000000..83da4fad329b4a34536cae5b79479c30c97ed91d GIT binary patch literal 1391 zcmZuxOKcle6g|%#&x{?XabL!13#GV-kMn5*1r=yMxFn6@;(X{BLkYiU`^A2VJ!8#` zsj^3c6)Se^*sw%&(Mlk?VF$}DSyYH35P}6%NU)*YH+BS3JF?&B+;i`{_uM!0%|Bn< z0I&={g9MTmj4VoX&Bt zsNFzC%0`}IpQpGDB_e5-e(J@sZ5$)k`FdN0ZMkdXK?ZA0c%mig{NFzsavo+#Eh~a; zoTPNCGK{qShYyDK$H?yXo4SN2C_q(;Mq3lUKQbgtYk~+wXyX(^V)c?)`tk3f?J0`r z_V+~Cw(&H1FSTV?g^^kMd1uIYHkQcq1Tmhcb=jliJ+pLk|NI38qZ}*v0##X+jdr0T zMbj+Z*=H$N{~Y9!Mj00^yp%-+YX>^%?oq{EYM|ez ze&z>)D}Q#A^pB_uQgr8RY>;VTVWF_!+(L!wC=imt%eZ90&7z7|7{=oc9(1R|dpcOh zqaek5J33j@*WlGGUelLsq^ZJQN9>ioZQsW042Mn~cr3g@1$Tw=NKs?SZ%T%-it^-o zzgw38`Z;ms$;$kzYeM%_p>cGpBNPQ*gXf}+T--wzscMd^eyaZabTd^m|Mdhzs=0M!bU z#y4p= 1) + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.print("Enter the number of elements: "); + int n = sc.nextInt(); + int[] arr = new int[n]; + System.out.println("Enter " + n + " integers in different lines:"); + for (int i = 0; i < n; i++) { + arr[i] = sc.nextInt(); + } + + // Call the getdifference method + int diff = getdifference(arr); + System.out.println("Difference between max and min: " + diff); + sc.close(); + } + public static int getdifference(int[] arr) { + if (arr == null || arr.length == 0) { + System.out.println("invalid array"); + } + int min = Integer.MAX_VALUE; + int max = Integer.MIN_VALUE; + + // Loop + conditionals + for (int i = 0; i < arr.length; i++) { + if (arr[i] < min) min = arr[i]; + if (arr[i] > max) max = arr[i]; + } + return max - min; + + } + } + diff --git a/source/Challenge4.java b/source/Challenge4.java new file mode 100644 index 0000000..3d27c07 --- /dev/null +++ b/source/Challenge4.java @@ -0,0 +1,2 @@ +public class Challenge4 { +} diff --git a/source/Employee.java b/source/Employee.java new file mode 100644 index 0000000..591e085 --- /dev/null +++ b/source/Employee.java @@ -0,0 +1,60 @@ +public class Employee { + private int id; + private String name; + private String role; + private double salary; + + // --- Constructor --- + public Employee(int id, String name, String role, double salary) { + this.id = id; + this.name = name; + this.role = role; + this.salary = salary; + } + + // --- Getters (to read values) --- + public int getId() { + return id; + } + + public String getName() { + return name; + } + + public String getRole() { + return role; + } + + public double getSalary() { + return salary; + } + + // --- Setters (to update values) --- + public void setId(int id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } + + public void setRole(String role) { + this.role = role; + } + + public void setSalary(double salary) { + if (salary >= 0) { // basic validation + this.salary = salary; + } else { + System.out.println("Salary cannot be negative!"); + } + } + + + public void displayInfo() { + System.out.println("Employee ID: " + id); + System.out.println("Name: " + name); + System.out.println("Role: " + role); + System.out.println("Salary: " + salary); + } +} diff --git a/source/Intern.java b/source/Intern.java new file mode 100644 index 0000000..8cbce11 --- /dev/null +++ b/source/Intern.java @@ -0,0 +1,35 @@ +public class Intern extends Employee{ + public static final double MAX_SALARY = 20000.0; + + // Constructor + public Intern(int id, String name, String role, double salary) { + // Set a temporary salary (0), then use our overridden setter + // so the validation (cap) is applied. + super(id, name, role, 0); + setSalary(salary); // will validate against MAX_SALARY + } + + // Override setSalary to enforce the cap + @Override + public void setSalary(double salary) { + if (salary < 0) { + System.out.println("Salary cannot be negative!"); + return; + } + if (salary > MAX_SALARY) { + System.out.println("Intern salary cannot exceed " + MAX_SALARY+"."); + super.setSalary(MAX_SALARY); + } else { + super.setSalary(salary); + } + } + + // (Optional) A helper to show it's an intern when printing + @Override + public void displayInfo() { + System.out.println("=== Intern ==="); + super.displayInfo(); + System.out.println("Intern Max Salary: " + MAX_SALARY); + } +} + diff --git a/source/challenge2.java b/source/challenge2.java new file mode 100644 index 0000000..b8cbb44 --- /dev/null +++ b/source/challenge2.java @@ -0,0 +1,45 @@ +public class challenge2 { + public static void findSmallest(int[] arr) { + if (arr == null || arr.length < 2) { + System.out.println("Need at least 2 elements"); + return; + } + + // Start by assuming first two numbers are smallest and second smallest + int smallest = arr[0]; + int secondSmallest = arr[1]; + + // Make sure smallest < secondSmallest + if (smallest > secondSmallest) { + int temp = smallest; + smallest = secondSmallest; + secondSmallest = temp; + } + + // Check rest of the array + for (int i = 2; i < arr.length; i++) { + int current = arr[i]; + + if (current < smallest) { + // Found new smallest, shift previous smallest to secondSmallest + secondSmallest = smallest; + smallest = current; + } else if (current < secondSmallest && current != smallest) { + // Found new second smallest (different from smallest) + secondSmallest = current; + } + } + + // Print results + System.out.println("Smallest: " + smallest); + System.out.println("Second smallest: " + secondSmallest); + } + + public static void main(String[] args) { + int[] data = {7, 2, 9, 2, 15, 8, 3}; + findSmallest(data); + } +} + + + diff --git a/source/main.java b/source/main.java new file mode 100644 index 0000000..bc18d2e --- /dev/null +++ b/source/main.java @@ -0,0 +1,17 @@ +public class main { + public static void main(String[] args) { + // Create Employee objects + Employee e1 = new Employee(1, "Swikriti", "Associate", 55000); + Employee e2 = new Employee(2, "Harsha", "Professor", 48000); + + // Display information + e1.displayInfo(); + System.out.println("----"); + e2.displayInfo(); + + // Update salary + e1.setSalary(60000); + System.out.println("Updated Salary for " + e1.getName() + ": " + e1.getSalary()); + } + +} diff --git a/source/main1.java b/source/main1.java new file mode 100644 index 0000000..77ea321 --- /dev/null +++ b/source/main1.java @@ -0,0 +1,11 @@ +public class main1 { + public static void main(String[] args) { + Intern i1 = new Intern(101, "Riya", "Intern - QA", 18000); + i1.displayInfo(); + + System.out.println("---- Trying to set salary above the cap ----"); + i1.setSalary(25000); // will be capped to 20000 + i1.displayInfo(); + } +} + diff --git a/source/main3.java b/source/main3.java new file mode 100644 index 0000000..8651ce7 --- /dev/null +++ b/source/main3.java @@ -0,0 +1,25 @@ +public class main3 { + public static void main(String[] args) { + // Create an array to hold 10 employees + Employee[] employees = new Employee[10]; + + // Fill with sample employees + employees[0] = new Employee(1, "Harsha", "Professor", 55000); + employees[1] = new Employee(2, "Swikriti", "Associate", 48000); + employees[2] = new Employee(3, "Abhishek", "Manager", 72000); + employees[3] = new Employee(4, "Diana", "Tester", 40000); + employees[4] = new Employee(5, "Sudha", "Teacher", 35000); + employees[5] = new Employee(6, "Saras", "HR", 45000); + employees[6] = new Employee(7, "Suyash", "Chemist", 50000); + employees[7] = new Employee(8, "Hannah", "Intern", 20000); + employees[8] = new Employee(9, "Isha", "Tech Lead", 80000); + employees[9] = new Employee(10, "Julia", "Product Owner", 90000); + + // Print all employees + System.out.println("=== Employee List ==="); + for (Employee e : employees) { + e.displayInfo(); + System.out.println("--------------"); + } + } +}