Skip to main content

Posts

Write a program that demonstrate inner class and static fields.

This program defines an outer class called OuterClass that contains a static field called staticField and an instance field called instanceField. It also defines two inner classes: InnerStaticClass, which has a method that prints the value of the static field, and InnerInstanceClass, which has a method that prints the value of the instance field. The main method creates an instance of the outer class, outer, and then creates instances of both inner classes, innerInstance and innerStatic. It then uses these instances to access the instance and static fields, respectively. Note that the InnerInstanceClass requires an instance of the outer class in order to be created, whereas the InnerStaticClass can be created without an instance of the outer class. This demonstrates the difference between inner instance classes and inner static classes. public class OuterClass { private static int staticField = 10 ; private int instanceField = 20 ; public static class In...
Recent posts

Write a program that demonstrate string operations using StringBuffer class.

This program demonstrates several operations that can be performed using the StringBuffer class. It creates a new StringBuffer object with the initial value "Hello, ", and then appends "world!" to the end of the string using the append() method. It then inserts "Java " into the middle of the string using the insert() method, replaces "Hello" with "Hi" using the replace() method, deletes the first three characters of the string using the delete() method, and reverses the order of the characters in the string using the reverse() method. public class StringBufferDemo { public static void main ( String [] args ) { // Create a new StringBuffer object StringBuffer sb = new StringBuffer ( "Hello, " ); // Append a string to the StringBuffer sb . append ( "world!" ); System . out . println ( sb ); // Insert a string into the middle of the StringBuffer ...

Write a program that demonstrate program structure of java with use of String function implementation.

This program demonstrates the use of String functions in Java. It prompts the user to enter a string, and then uses the length() method to determine the length of the string, the toUpperCase() method to convert the string to uppercase, and the substring() method to extract a substring from the original string. Note that the substring() method requires two parameters: the starting index and the ending index. The program prompts the user to enter these values and then uses them to extract the desired substring. This program demonstrates the basic structure of a Java program, including the use of imports, class definitions, method definitions, and user input/output. import java.util.Scanner ; public class StringFunctionDemo { public static void main ( String [] args ) { // Create a Scanner object to read user input Scanner scanner = new Scanner ( System . in ); // Prompt the user to enter a string System . out . print ( "Enter...

Write a program that demonstrate program structure of java with use of Logical class implementation.

This program defines a class called Logical that contains two methods (isEven and isOdd) to determine if a number is even or odd. The main method uses the Scanner class to read user input and then calls the isEven method to determine if the number entered by the user is even or odd. The program then outputs a message indicating whether the number is even or odd. Note that the isOdd method uses the ! (logical NOT) operator to invert the result of the isEven method. This demonstrates the use of logical operators in Java. // Import the Scanner class to read user input import java.util.Scanner ; // Define the Logical class public class Logical { // Define a method to determine if a number is even public static boolean isEven ( int num ) { return num % 2 == 0 ; } // Define a method to determine if a number is odd public static boolean isOdd ( int num ) { return ! isEven ( num ); } // Define the main meth...

Write a program that demonstrate program structure of java with use of arithmetical implementation.

In this program, we first define a class called ArithmeticalOperations. Within this class, we have a method called main which is the entry point of the program. The main method takes an array of strings as its argument, but we don't use it in this program. Inside the main method, we declare two integer variables a and b and initialize them with values 10 and 5, respectively. We then perform several arithmetic operations on these variables and store the results in separate integer variables called sum, difference, product, quotient, and remainder. public class ArithmeticalOperations { public static void main ( String [] args ) { int a = 10 ; int b = 5 ; int sum = a + b ; int difference = a - b ; int product = a * b ; int quotient = a / b ; int remainder = a % b ; System . out . println ( "Sum: " + sum ); System . out . println ( ...

Find mean, mode and median of a vector in python

Find mean, mode and median of a vector in python To find the mean, mode, and median of a vector in Python, you can use the following implementation: def find_max_index (arr): if len(arr) == 1 : return 0 else : max_index = find_max_index(arr[ 1 :]) return (max_indexdef factorial(n): if n == 0 : return 1 else : return n * factorial(n - 1 ) n = int(input( "Enter a Number : " )) fact = factorial(n) print (fact) + 1 ) if arr[ 0 ] < arr[max_index + 1 ] else 0 arr = [ 4 , 6 , 2 , 8 , 5 ] max_index = find_max_index(arr) print (max_index) # Output: 3 This code first imports the statistics module, which provides functions for calculating statistical properties of a dataset. It then defines a vector with some sample data. You can replace this with your own vector. To calculate the mean of the vector, it first calculates the sum of all the elements in the vector using...

Find factorial on given number using recursion in python

Find factorial on given number using recursion in python def find_max_index (arr): if len(arr) == 1 : return 0 else : max_index = find_max_index(arr[ 1 :]) return (max_indexdef factorial(n): if n == 0 : return 1 else : return n * factorial(n - 1 ) n = int(input( "Enter a Number : " )) fact = factorial(n) print (fact) + 1 ) if arr[ 0 ] < arr[max_index + 1 ] else 0 arr = [ 4 , 6 , 2 , 8 , 5 ] max_index = find_max_index(arr) print (max_index) # Output: 3 This function takes a non-negative integer n as an argument and returns its factorial. It first checks if n is 0, in which case it returns 1 as the factorial of 0 is 1. Otherwise, it makes a recursive call to factorial with n-1 as the argument. This recursively calculates the factorial of n-1. Finally, it multiplies n with the factorial of n-1 to get the factorial of n.