100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Other

Simple Example Codes of Java in Object-Oriented Programming

Rating
-
Sold
-
Pages
16
Uploaded on
26-06-2024
Written in
2023/2024

Simple Example Codes of Java in Object-Oriented Programming

Institution
Course










Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Institution
Course

Document information

Uploaded on
June 26, 2024
Number of pages
16
Written in
2023/2024
Type
Other
Person
Unknown

Subjects

Content preview

NAME AND AGE
package Demo1;
import java.util.*;
public class example1 {

public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);

System.out.print("Enter Name: ");
String name = sc.nextLine();

System.out.print("Enter Age: ");
int age = sc.nextInt();

System.out.println("Name: " + name + '\n' + "Age: " + age);

}
}

RESULTS:
Enter Name: Thea
Enter Age: 19
Name: Thea
Age: 19

SUM
package Demo1;
import java.util.*;
public class example1 {

public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);

System.out.print("Enter first number: ");
int fnum = sc.nextInt();

System.out.print("Enter second number: ");
int snum = sc.nextInt();

int sum = fnum + snum;

System.out.println("Sum: " + sum);

}
}

RESULTS:
Enter first number: 354
Enter second number: 435
Sum: 789

,SPEED
package Demo1;
import java.util.*;
public class example1 {

public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);

int distance = 1000;

System.out.print("Enter the time of the car: ");
int time = sc.nextInt();

int speed = distance/time;

System.out.println("Speed: " + speed);

}
}

RESULTS:
Enter the time of the car: 100
Speed: 10

CIRCUMFERENCE
package Demo1;
import java.util.*;
public class example1 {

public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);

double pi = 3.14;

System.out.print("Enter the radius of the circle: ");
int radius = sc.nextInt();

double circumference = 2 * (pi * radius);

System.out.println("Circumference: " + circumference);

}
}

RESULTS:
Enter the radius of the circle: 2
Circumference: 12.56

RADIUS
package Demo1;
import java.util.*;
public class example1 {

, public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);

double pi = 3.14;

System.out.print("Enter the circumference of the circle: ");
double circumference = sc.nextDouble();

double radius = circumference / (2 * pi);

System.out.println("Radius: " + radius);

}
}

RESULTS:
Enter the circumference of the circle: 12.56
Radius: 2.0

Connected classes
//1st class name: example1
package Demo1;
import java.util.*;
public class example1
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner scan = new Scanner (System.in);

System.out.print("Enter first num: ");
int fnum = scan.nextInt();

System.out.print("Enter second num: ");
int snum = scan.nextInt();

System.out.println("");

test test = new test ();

test.addNum(fnum, snum);

}
}

//2nd class name: test
package Demo1;
public class test {

public int sum;

void addNum (int num1, int num2) {
$3.99
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
jrrythm

Also available in package deal

Get to know the seller

Seller avatar
jrrythm SISC
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
1 year
Number of followers
0
Documents
16
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions