






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The Scanner looks for tokens in the input. A token is a series of characters that ends with what Java calls whitespace. A whitespace character can be a blank, a tab character, a carriage return. Thus, if we read a line that has a series of numbers separated by blanks, the scanner will take each number as a separate token.. The numeric values may all be on one line with blanks between each value or may be on separate lines. Whitespace characters (blanks or carriage returns) act as separators. The next method returns the next input value as a string, regardless of what is keyed. For example, given the following code segment and data
Typology: Lab Reports
1 / 11
This page cannot be seen from the preview
Don't miss anything!
Department of Computer Engineering
Batch: Roll No.: 16 Experiment / assignment / tutorial No. Grade: AA / AB/ BB/ BC / CC / CD /DD Signature of the Staff In-charge with date
a static function check_per , which checks if the number is a perfect number or not and sends the result back to the main function which counts and displays the perfect numbers within that range. Variations : Implementation of Program with One class Accessibility with static and non-static methods within class and outside class.
CO2: Explore arrays, vectors, classes and objects in C++ and Java Books/ Journals/ Websites referred:
Department of Computer Engineering either the keyboard or file without having to convert them from strings and determine if there are more values to be read. Scanner in = new Scanner(System.in); // System.in is an InputStream Numeric and String Methods Method Returns int nextInt() Returns the next token as an int. If the next token is not an integer,InputMismatchException is thrown. long nextLong() Returns the next token as a long. If the next token is not an integer,InputMismatchException is thrown. float nextFloat() (^) Returns the next token as a float. If the next token is not a float or is out of range, InputMismatchException is thrown. double nextDouble() Returns the next token as a long. If the next token is not a float or is out of range, InputMismatchException is thrown. String next() Finds and returns the next complete token from this scanner and returns it as a string; a token is usually ended by whitespace such as a blank or line break. If not token exists,NoSuchElementException is thrown. String nextLine() Returns the rest of the current line, excluding any line separator at the end. void close() Closes the scanner. The Scanner looks for tokens in the input. A token is a series of characters that ends with what Java calls whitespace. A whitespace character can be a blank, a tab character, a carriage return. Thus, if we read a line that has a series of numbers separated by blanks, the scanner will take each number as a separate token.. The numeric values may all be on one line with blanks between each value or may be on separate lines. Whitespace characters (blanks or carriage returns) act as separators. The next method returns the next input value as a string, regardless of what is keyed. For example, given the following code segment and data
Department of Computer Engineering Algorithm:
count++;
break;
Department of Computer Engineering break;
sum += i ;
} Variation 1:
}
Department of Computer Engineering sum += i ;
} Variation 3:
count++;
break;
break;
Department of Computer Engineering if (sc. hasNextInt()) { int n = sc. nextInt(); if (n > 0 )
} System. out. println( "Invalid input. Please enter a positive integer."
sc. nextLine(); } } public static boolean is_perf( int n) { int sum = 0 ; for (int i = 1 ; i < n; i ++) { if (n % i == 0 ) sum += i ; }
} }
public static void main( String args[]) {
} } Output: All produce the same result: This program will print all perfect numbers in a given range. Enter lower limit: 1 Enter upper limit: 10000 6 28 496 8128 There are 4 perfect numbers in the given range. Do you want to continue? (1 for yes, 2 for no) Enter your choice: 1
Department of Computer Engineering Post Lab Descriptive Questions: Q.1 Write a program to find the area and circumference of a circle using two classes.
}
}
}
break;
}
Department of Computer Engineering Output: 1 1 1 2 1 3 2 1 3 1 3 2
Q3. Why is Java known as a platform independent language? Ans. The java language is known as a platform independent language because any code which is written in java is converted to an intermediate form known as byte code which is independent of the machine on which the program is run or compiled on. It is the same for all platforms. Whenever the program is to be executed, the byte code is converted to machine code by the Java virtual machine (JVM) which converts the java compiled byte code into machine code which is different for each machine. Q4. Write a recursive static method for calculation of gcd of a number. public static void gcd( int a, int b) { if (b == 0 ) { System. out. println( "GCD is " + a); else } {
} }