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

jpr answers

java programming for diploma 3 year

Uploaded by

adityadhumal2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

jpr answers

java programming for diploma 3 year

Uploaded by

adityadhumal2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Write a program to accept marks if (percentage >= 33 &&


and find grade using if percentage <= 59) {
statement grade = 'D';
class GFG { }
public static void main(String args[]) else {
{ grade = 'F';
an array }
int marks[] = { 25, 65, 46, 98, 78, 65 }; }
int max_marks = marks.length * 100; }
0 }
int total = 0; System.out.println(grade);
F }
char grade = 'F'; }
to find the sum. 2. Write a program to implement
for (int i = 0; i < marks.length; i++) { different types of constructors to
perform addition of complex
total += marks[i];
number
}
class ComplexNumber {
double percentage
int real, image
= ((double)(total) / max_marks) *
public ComplexNumber(int r, int i)
100;
{
if (percentage >= 90) {
this.real = r;
grade = 'A';
this.image = i;
}
}
else {
public void showC()
if (percentage >= 80 && percentage
{
<= 89) {
System.out.println(this.real + " +i " +
grade = 'B';
this.image);
}
}
else {
public complex add(ComplexNumber,
if (percentage >= 60 && ComplexNumber);
percentage <= 79) {
}
grade = 'C';

else {
3. Write a java program to }
implement following functions }
of string :
(1)Calculate length of string
(2)compare between strings
3)class TestStringConcatenation1{
(3)Concatenating strings
public static void main(String args[]){
1)public class Test {
String s="Sachin"+" Tendulkar";
// main function
System.out.println(s);//Sachin Tendulkar
public static void main(String[] args)
}
{
}
String str = "GeeksforGeeks";
4. Write a program to find largest
System.out.println("The size of "
number in array
+ "the String is "
public class LargestElement_array {
+ str.length());
public static void main(String[] args) {
}
int [] arr = new int [] {25, 11, 7, 75,
} 56};

2)import java.io.*; int max = arr[0];

class GFG { for (int i = 0; i < arr.length; i++) {

public static void main(String[] args) max

{ if(arr[i] > max)

String s1 = "abc"; max = arr[i];

String s2 = "xyz"; }

int len1 = s1.length(); System.out.println("Largest element


present in given array: " + max);
int len2 = s2.length();
}
if (len1 == len2) {
}
System.out.println(

"The length of both the strings


5. Develop a program to print
command line argument using
are equal and is " for loop
class GFG {
+ len1);
public static void main(String[] args)
}
{
else {
System.out.println(“your first
System.out.println(
argument is:”+args[0]);
"The length of both the strings are
}
not equal");
}
}
5)Write a single program to i.dis();
implement inheritance and
i=new B();
polymorphism in java
i.dis();
class A
i=new C();
{
i.dis();
void dis()
}
{
}
System.out.println("From A");
6. Write a program to
}
implement user defined
} packages in terms of
creating a new package and
class B extends A
importing the same
{
public class gfg {
void dis()
public void show()
{
{
System.out.println("From B");
System.out.println("Hello
} geeks!! How are you?");
} }
class C extends B public static void main(String
args[])
{
{
void dis()
gfg obj = new gfg();
{
obj.show();
System.out.println("From C");
}
}
}
}
class He
{
public static void main(String ar[])
{
A i;
i=new A();
7. Write a Applet program to set public void paint(Graphics g)
background with red colour and
{
foreground with blue colour
import java.applet.Applet; g.drawString(“Welcome to java”,25,50);
import java.awt.Color;
import java.awt.Graphics; }
public class Back extends Applet }
{
public void init() The life cycle of a Java applet includes
{ the steps of being loaded, initialised,
setBackground(Color.red); shown, and finally destroyed. There are
setForeground(Color.blue); four stages in an applet's life cycle that
} the browser or applet viewer will trigger:
public void paint( Graphics g ) init(), start(), stop() and destroy()
{ 9. Write a java program to extend
g.drawString(" Welcome to interface assuming suitable data?
Coding Atharva ",10,100);
} interface A {
} void funcA();
/*
<applet code="Back" height=200 ; }
width = 200 > interface B extends A {
</applet>
*/ void funcB();
8. Define applet. Write a program
}
to create an applet to display
message “Welcome to java class C implements B {
Applet”
public void funcA() {
Java applet is a small dynamic Java
System.out.println("This is funcA");
program that can be transferred via the
Internet and run by a Java-compatible }
Web browser. The main difference
between Java-based applications and public void funcB() {
applets is that applets are typically System.out.println("This is funcB");
executed in an appletviewer or
Javacompatible Web browser. All }
applets }
import the java.awt package. // public class Demo {
import java.applet.*; public static void main(String args[]) {
import java.awt.*; C obj = new C();
public class WelcomeJava extends obj.funcA();
Applet
obj.funcB(); }}
{
1) Explain any four features of java with example
Object Oriented — Java is an object-oriented programming language
because it treats everything as an object. The entire program code and data
resides within objects and classes. This enables us to easily use and extend
the object model…..Robust — Java is a robust and reliable programming
language. It has strict compile time and runtime checking of data types.
Memory allocation and de-allocation is automatic in Java, so there is less for
the programmer to worry about. …..Platform Independent — The concept of
"Write once, run anywhere", is one of the most important and powerful
features of Java. Java applications can run on any platform that has a
corresponding JVM installed on it…..Simple — Java is simple and easy to
learn programming language. The removal of many unreliable features of C
and C++ (its predecessors) has simplified its understanding and
implementation.
2) Describe concept of type casting and Explain its types with proper syntax
and example
Typecasting is the process of converting the value of a single data type (such
as an integer [int], float, or double) into another data type.
Explicit Conversion…….In this kind of typecasting, we are supposed to
explicitly define the data type in which we want to convert any value. unlike
implicit conversion, it has to be mentioned the data type before the operation
or value in which it has to be converted.Example-Int a = (int) 7/6
Implicit Conversion…..In this mode of typecasting, the value of one data type
could be converted to the other on its own, and we will not be required to
mention anything. It all happens on its own or automatically.Example:Float a
= 7/6.

3) State & explain scope of variable with an example

Scope of a variable is the part of the program where the variable is


accessible. Like C/C++, in Java, all identifiers are lexically (or statically)
scoped, i.e.scope of a variable can be determined at compile time and
independent of function call stack. Member variable-These variables must be
declared inside class (outside any function). They can be directly accessed
anywhere in class. Let’s take a look at an example: public class Test
{
// All variables defined directly inside a class
// are member variables
int a;
private String b;
void method1() {....}
int method2() {....}
char c;
}
Variables declared inside a method have method level scope and can’t be
accessed outside the method.

public class Test


{
void method1()
{
// Local variable (Method level scope)
int x;
}
}

4)Explain any four method of String with syntax and example


char charAt(int index)- It returns the character value at the particular index. The index
value lie between 0 to length () – 1.
String substring(int beginIndex)- It returns a substring from a specified index.
int length()- It returns string length.
String substring(int beginIndex, int endIndex)- It returns substring for begin and end index.

5)Describe the visibility controls in Java with suitable example

In Java, visibility control refers to the ability to restrict the access to class
members (i.e., fields, methods, and inner classes) from other classes or code
outside the current class.For example, the Java Servlet API (javax.servlet)
getServletContext() returns an implementation of
javax.servlet.ServletContext, which is typically an immutable proxy to satisfy
the ServletContext framework promises. It is most possible that a separate
version of the javax.servlet.ServletContext specification operates on the
application server.

6) What is Interface? Describe syntax, feature & need of an interface

An interface is a reference type and is similar to the class. Along with


abstract methods, an interface may also contain constants, default methods,
static methods, and nested types.n Java, an interface specifies the behavior
of a class by providing an abstract type. As one of Java's core concepts,
abstraction, polymorphism, and multiple inheritance are supported through
this technology. Interfaces are used in Java to achieve abstraction.

You might also like