Sunteți pe pagina 1din 5

Java assignment

a) Yes, you can overload main method in Java. But the program doesn't execute the overloaded
main method when you run your program, you have to call the overloaded main method from the
actual main method.
b) A constructor returns a new instance of the class it belongs to, even if it doesn't have an explicit

return statement
c) Use a static initializer block to print the message. This way, as soon as your class is loaded the
message will be printed.
However exception is shown later.
d)
1. this keyword can be used to refer current class instance variable.
2. This () can be used to invoke current class constructor.
3. this keyword can be used to invoke current class method (implicitly)
4. this can be passed as an argument in the method call.
5. this can be passed as argument in the constructor call.
6. this keyword can also be used to return the current class instance.
e) when we give value as float it shows error cant convert float to double when we give double displays
infinity as output

f)
When you use a string literal the string can be interned, but when you use new String ("...") you
get a new string object.
In this example both string literals refer the same object:
String a = "abc";
String b = "abc";
System.out.println(a == b) ; // true

Here, 2 different objects are created and they have different references:
String c = new String("abc");
String d = new String("abc");
System.out.println(c == d); // false

g. 3 is a promitive int and therefore java automatically casts the other operand (0.1) to an int as
well (which is 0) before doing the actual multiplication.

h.because factorial of a number is n*(n-1).1


i.no because there must be method to call and return value of method so not possible.
j.ONE12TWOTHREE34FOURFIVE5

programing assignment 1:
import java.util.Scanner;
public class program1 {
public static void main(String args[])
{
int n;
System.out.println("Enter a number");
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
while(n>1)
{
if(n%2==0)
{
n=n/2;
System.out.println(n);
}
else if(n%2!=0)
{
n=3*n+1;
System.out.println(n);
}

}
}

Programmig assignment 2:
import java.util.Scanner;
public class program2 {
public static void main(String args[])
{

int n=0,t;
int i;
int a[]=new int[100];
b: for(i=0;i<100;i++)
{

System.out.println("enter elements");
Scanner s=new Scanner(System.in);
a[i]=s.nextInt();
n++;
if(a[i]==0)
break b;
}
for(i=0;i<n;i++)
{
if(a[i]>a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
}
System.out.println(a[0]);
System.out.println(a[n]);
}
}

Programmming assignment 3:
public class program3 {
{

public static void main(String[] args)


{
int[] invalue = {2,4,5,2,6};
int num_value=5;
double tot=0;
double mean=0;
for(int i=0; i<num_value; i++)
{
tot = tot+invalue[i];
}
mean = tot/num_value;
System.out.println("The mean value is: "+mean);
//Median calculation
double median = 0;
double mid=0;
if(num_value%2 == 0)
{
int temp=(num_value/2)-1;
for(int i=0;i<num_value;i++)
{
if(temp==i || (temp+1)==i)
{
mid=mid+invalue[i];
}
}
mid=mid/2;
System.out.println("Median value is: "+mid);
}
else
{
int temp=(num_value/2);

for(int i=0;i<num_value;i++)

if(temp==i)

mid=invalue[i];
System.out.println("Median value: "+mid);
}

}
}
//Mode calculation
int i,j,z, tmp, maxCount, modeValue;
int[] tally=new int[num_value];
for(i=0;i<num_value;i++)
{
for(j=0;j<num_value-i;j++)

{
if(j+1!=num_value)
{
if(invalue[j]>invalue[j+1])
{
tmp=invalue[j];
invalue[j]=invalue[j+1];
invalue[j+1]=tmp;
}
}
}
}
for (i = 0; i < num_value; i++)
{ for(z=i+1;z<num_value;z++)
{
if(invalue[i]==invalue[z])
{
tally[i]++;
}
}
}
maxCount = 0;
modeValue = 0;
for (i = 0; i <num_value; i++)
{
if (tally[i] > maxCount)
{
maxCount = tally[i];
modeValue = invalue[i];
}
}
System.out.println("Mode value is :"+modeValue);
}
}

Programming assignment 4:
/* Java Program Example - Remove/Delete Vowels from String */
import java.util.Scanner;
public class JavaProgram
{

public static void main(String args[])


{
String str, r;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a String : ");
str = scan.nextLine();
System.out.print("Removing Vowels from String [" +str+ "]\n");
r = removeVowels(str);
System.out.print("Vowels Removed from the Entered String Successfully..!!\nNow the
String is :\n");
System.out.print(r);
}
private static String removeVowels(String s)
{
String finalString = "";
int i;

for(i=0; i<s.length(); i++)


{
if (!isVowel(Character.toLowerCase(s.charAt(i))))
{
finalString = finalString + s.charAt(i);
}
}
return finalString;

private static boolean isVowel(char c)


{
String vowels = "aeiou";
int i;
for(i=0; i<5; i++)
{
if(c == vowels.charAt(i))
{
return true;
}
}
return false;
}

}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

S-ar putea să vă placă și