0% found this document useful (0 votes)
24 views

Lab3. Java Fundamentals and Control Structures

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
24 views

Lab3. Java Fundamentals and Control Structures

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
‘DAI HOC BA NANG . TRUONG DAI HOC CONG NGHE THONG TIN VA TRUYEN THONG VIET - HAN SSS Vietnam - Korea University of Information and Communication Technology LAB 3 Chapter 3. Java fundamentals and control structures 1. Write an OOP program to get a integer from the keyboard and check if it positive, negative or zero? import java.util.Scanner; public class Exercise] { public static void main(string[] args) { Scanner k=new Scanner (System. in); System.out.print ("Input number : "); int n=k.nextint (7 if (n==0) System. out.print ("Zero") ; else if(n>0) System. out.print ("Number is positive’ else Systcm.out.print ("Number is ncgative"); — ao = x%/ Ra eBI = 0-5-- 5 Input number = Number is positive 2. Write an OOP program to solve a quadratic equation (ax2 + bx + ¢ =0) import java.util.Scanner; public class Quadractigquation { public static void main(Sleing[] azys) { float a,b,c, x1,x2,del; Scanner keyboard = new Scanner (System. in); System. out.print ("a ="); a = keyboard.nextFloat (); System.out.print("b = "); b = keyboard.nextFloat (); System.out.print ("c = "); c = keyboard.nextFloat (); OBJECT ORIENTED PROGRAMMING = YKU HUYNH CONG PHAP, Assoc., PhD. 1 ‘DAI HOC BA NANG VK TRUONG DAI HOC CONG NGHE THONG TIN VA TRUYEN THONG VIET - HAN SS Vietnam - Korea University of Information and Communication Technology del b*b = 4*atc; if (del <0) System.amt.printin ("Rants are compler and different"); else if (de ( x1=x2=-b/ (2*a) 7 System.out.print1n(*Roots are real and same: "+x1); ) else //means del > 0 ) float) (-b+Math. sqrt (del)) /(2*a); float) (-b-Math. sqrt (del)) /(2*a): System.out.print1n("Roots are real and different: \n x1s"+x14"\n x2="4x2)7 cots are real and different: x1=-0.33333334 x2=-2.0 Requirement: Students must complete the code above for complex roots 3. Write an OOP program allowing users to input three numbers (a, b, e) from keyboard, Check if a, b, c numbers are 3 edges of a triangle? If yes, print out the type of triangle? import java.util.Scanner; public class TriangleCheck ( public static void main(Strina{] aras) { float a,b,c? ® ban phim new Scanner (System. in); //nhdp adv 1i6 Scanner keyboard system. out.print ("a = a = keyboard.nextFloat (); System.out.print ("b ="); b = keyboard.nextFloat ()i OBJECT ORIENTED PROGRAMMING = YKU HUYNH CONG PHAP, Assoc., PhD. 2 ‘DAI HOC BA NANG . TRUONG DAI HOC CONG NGHE THONG TIN VA TRUYEN THONG VIET - HAN SSS vietnam - Korea University of Information and Communication Technology System. ant print ("co ="); c = keyboard.nextFloat (); /*Check if 3 triangle edges are valid*/ if ((atb>c) && (atc>b) && (bt+c>a)&&(a>0) && (b>0)&&(c> 0)) { System.out.print("a, b, c are valid \n"); /*Get_ type of the triangle*/ if (( c)) System. out.print1n ("Equilateral triangle") else system. out.print1n("Isosceles triangle") else if (((atatb# *c) 6a (a HL ((ctctb4b: System. out.println("Isosceles right triangle"); else if ((a*a==b*btc*c) | | (b¥b==a*atc*c) | | (c*c System.out.printIn("Right triangle"); else System. out.print1n ("Triangle"); *atb*b)) t else //belong to the first if System.out.println("\"a, b, c are NOT valid 4. Write an OOP program to sum all even numbers from 2 to N. import java.util.Scanner; public class SumBven { public static void main(string[] args { Scanner keyboard = new Scanner (System. in); System.out.print ("N = "); int N = keyboard.nextInt (); ant sum -0; ) OBJECT ORIENTED PROGRAMMING = VYKU HUYNH CONG PHAP, Assoc., PhD. 3 DAI HOC DA NANG VK TRUONG DAI HOC CONG NGHE THONG TIN VA TRUYEN THONG VIET - HAN Vietnam - Korea University of Information and Communication Technology System.out.printin("sum — "+sum); , ) 5. Write an OOP program to check whether N (N>0), which got from the keyboard, is a prime number or not? import java.util.Scanner; public class PrimeNumber { public static void main(String[] args) { int N3 int i; ew Scanner(System. in); Scanner keyboard = system.out.print ("N N = keyboard.nextInt(); /*kiém tra sé nguyén to*/ for (i=2;i<=Math.round(Math.sqrt(N));i++) if (N%is=@) break;/*Néu chia hét cho mot sé i thi N khong phai sé nguyén to*/ /*Néu i nhé hon hogc bang cén bac 2 N cé nghta vong Lap bi két thic bang cdu Lénh break*/ if (4 <= Math.round(Mata.sqrt(N))) System.out.print("N is not a prime number else System.out.print("N is a prime number "); } 5 Ee=5-5- [htios one Denn Ooo ee one ner echon omni yen A WADA N=7 *N is a prime number DO IT YOURSELF 1. Write an application that prints all of integer the numbers between I (included) and 100 (included) using the “while” loop) 2. Write an application that prints all of the integer numbers between I (included) and 100 (included) using the “do..while” loop. ORIENTED PROGRAMMING VKU HUYNH CONG PHAP, Assoc., PhD. 4 OBIE DAI HOC DA NANG VK TRUONG DAI HOC CONG NGHE THONG TIN VA TRUYEN THONG VIET - HAN Vietnam - Korea University of Information and Communication Technology 3. Write an application that prints all of the integer numbers between I (included) and 100 (included) using the “for” leop. 4, Write an application that prints all of the even numbers between I (included) and 100 (included) using the “while” loop. 5. Write an application that prints all of the even numbers between | (included) and 100 (included) using the “do..while” loop. 6. Write an application that prints all of the even numbers between I (included) and 100 (included) using the “for” leop. 7. Write an application that calculates the sum of all the integer numbers between 1 (included) and 100 (included), 8. Write an application that calculates and prints the average of all the integer numbers between I (included) and 190 (included). 9. Write an application that calculates and prints the biggest number between 1 (included) and 100 (included) that divides in 7 without a residual. 10. Write an application that calculates the sum of all the numbers between 1 (included) and 100 (included) that divide in 7 without a residual. 11. Write an application that prints all of the integer numbers between I (included) and 1000 (included) and near each one of them (each number will be printed in anew line) it prints EVEN or UNEVEN. 12. Write an application that prints a rectangle of stars as follows: ereeeeree saseee ene ere eee ee seeeee eee errrrerie 13. Write an application that prints a shape of stars as follows: ee ee ee ke oe ee Ree eee eenieniemrenrs, ee 14, Write an application that prints to the screen the following: es * * eEetete) * ORIENTED PROGRAMMING VKU HUYNH CONG PHAP, Assoc., PhD. 5 DAI HOC DA NANG VK TRUONG DAI HOC CONG NGHE THONG TIN VA TRUYEN THONG VIET - HAN Vietnam - Korea University of Information and Communication Technology 15, Write an application that prints to the screen the following: es eRe * 16, Write an application that prints to the screen the following series: 0, 3, 8, 15, 24, 35, 48, 17. Write an application that prints to the screen the following series: 3 7 15, 31, 63, 18. Write an application that prints to the screen the following series: 1, 7, 16, 37, 79, 173... The application should print the first 10 numbers of the series. 19. Write an application that prints to the screen the following series: 1, 3, 9, 27, 81, 243 The application should print the first 10 numbers of the series. 20. The following application prints the factorial of 6. The computation of 6! is done using a static method that calculates the factorial of the number that it gets. Complete the missing lines. class FactorialA pplication { public static void main(String args{]) { Jong number, result; number = result = factorial(number); System-out printIn(“The factorial of 6 is : “ + result); } public static long factorial(long value) { long result = 1; ORIENTED PROGRAMMING VKU HUYNH CONG PHAP, Assoc., PhD. 6 DAI HOC DA NANG . TRUONG DAI HOC CONG NGHE THONG TIN VA TRUYEN THONG VIET - HAN Vietnam - Korea University of Information and Communication Technology /ladd the missing lines here return result; } 21, Develop a stand-alone application that prints each one of the numbers between and FFFF (the numbers should be printed in Hexadecimal base). 22. Develop a stand-alone application that prints each one of the numbers between 0 and 777 (the numbers should be printed in Octal base). OBJECT ORIENTED PROGRAMMING VKU HUYNH CONG PHAP, Assoc., PhD. 7

You might also like