Java Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

MORE, MORE SUM

import java.util.*;
public class Main{
public static void main (String[] args){
Scanner sc = new Scanner (System.in);
System.out.print("Enter x: ");
int x = sc.nextInt();
System.out.print("Enter y: ");
int y = sc.nextInt();
System.out.print("Enter z: ");
int z = sc.nextInt();
System.out.print("Sum: "+ (x+y+z));
}
}

FAHRENHEIT TO CELSIUS
import java.util.*;
public class Main{
public static void main (String[] args){
Scanner sc = new Scanner (System.in);
System.out.print("Enter tempereture in Fahrenheiht: ");
double f = sc.nextDouble();
System.out.printf("Temperature in Celsius: %.2f", ((f-32)*5/9));
}
}

REWARD MONEY
import java.util.*;
public class Main{
public static void main (String[] args){
Scanner sc = new Scanner (System.in);
System.out.print("Enter the reward money: ");
int m = sc.nextInt();
if(m<100){
System.out.print("That's too low");
} else if (m>1000){
System.out.print("That's too much");
}else
System.out.print("That's enough");
}
}
COUNTDOWN WITH A FOR LOOP
import java.util.*;
public class Main{
public static void main (String[] args){
Scanner sc = new Scanner (System.in);

System.out.print("Enter a positive integer: ");


int p = sc.nextInt();
for(int i = p; i>=0; i--){
System.out.println(i);
}
}
}

NUMBER TRIANGLE
import java.util.*;
public class Main{
public static void main (String[] args){
Scanner sc = new Scanner (System.in);

System.out.print("Enter a positive integer: ");


int n = sc.nextInt();
printDecendingNumbers(n);

}
public static void printDecendingNumbers(int n){
for(int i = p; i>=0; i--){
System.out.println(i);
}

}
}
ENCAPSULATION PRACTICE I
import java.util.*;
public class Basta{
private String name;
private int year;
private String publisher;
public void setter(){
Scanner sc = new Scanner(System.in);
System.out.print("Enter game name: ");
this.name = sc.nextLine();
System.out.print("Enter year published: ");
this.year = sc.nextInt();
sc.nextLine();
System.out.print("Enter game publisher: ");
this.publisher = sc.nextLine();
}
public String getName(){
return name;
}
public int getYear(){
return year;
}
public String getPublisher(){
return publisher;
}
}

import java.util.*;
public class Main{
public static void main (String[] args){
Scanner sc = new Scanner (System.in);

Basta obj = new Basta();


obj.setter();

System.out.println(obj.getName()+" is published on "+obj.getYear()+ " by "+ obj.getPublisher());

}
}
INHERITANCE PRACTICE I
import java.util.*;
public class Main{
public static void main (String[] args){
Scanner sc = new Scanner (System.in);

Student student = new Student();

student.setter();
student.displayDetails();

}
}

import java.util.*;
public class Student extends Person{
String school;
int id;
int yearStarted;

public void setter(){


Scanner sc = new Scanner(System.in);

System.out.print("Enter School: ");


this.school = sc.nextLine();
System.out.print("Enter Student ID: ");
this.id = sc.nextInt();
setName();
System.out.print("Enter Year: ");
this. yearStarted = sc.nextInt();
}

public String getSchool(){


return school;
}
public int getId(){
return id;
}
public int getYearStarted(){
return yearStarted;
}
public void displayDetails(){
System.out.print("Student from "+ getSchool()+" with ID "+getId()+" is "+getFirstName()+"
"+getLastName()+" who started on "+getYearStarted());
}
}

import java.util.*;

public class Person{


Scanner sc = new Scanner(System.in);
String firstName;
String lastName;

public void setName(){


System.out.print("Enter First Name: ");
this.firstName = sc.nextLine();
System.out.print("Enter Last Name: ");
this.lastName = sc.nextLine();
}

public String getFirstName(){


return firstName;
}
public String getLastName(){
return lastName;
}

}
ARRAY TRANSVERSAL QUIZ
import java.util.Scanner;
import java.text.DecimalFormat;
public class ArraySumAndAverage {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
DecimalFormat dc= new DecimalFormat("###.00");

System.out.print("Enter the size: ");


int size = scanner.nextInt();

int[] array = new int[size];

for (int i = 0; i < size; i++) {


System.out.print("Enter element " + (i + 1) + ": ");
array[i] = scanner.nextInt();
}
int sum = 0;
for (int i = 0; i < size; i++) {
sum += array[i];
}
double average = (double) sum / size;
System.out.println("Sum: " + sum);
System.out.println("Average: " + dc.format(average));
}
}
INTERFACE PRACTICE I
import java.util.*;
import java.text.*;
public class Main{
public static void main (String[] args){
Scanner sc = new Scanner (System.in);
DecimalFormat df = new DecimalFormat("###.00");
System.out.print("Enter shape(T/R): ");
char shape = sc.next().charAt(0);
System.out.print("Enter base: ");
double base = sc.nextInt();
System.out.print("Enter height: ");
double height = sc.nextInt();
switch (shape){
case 'T' :
Triangle triangle = new Triangle ();
System.out.print("Area: "+df.format(triangle.area(base,height)) );
break;
case 'R' :
Rectangle rectangle = new Rectangle ();
System.out.print("Area: "+ df.format(rectangle.area(base,height)) );
break;
}

}
}
public interface Polygon{
public double area(double base, double height);
}

public class Rectangle implements Polygon{


@Override
public double area (double base, double height){
return base * height;
}
}

public class Triangle implements Polygon{


@Override
public double area(double base, double height){
return (base*height)/2;
}
}
EXCEPTIONS PRACTICE I
import java.io.*;
import java.util.*;
public class Main{
public static void main (String[] args){
Scanner sc = new Scanner (System.in);

System.out.print("Enter x: ");
int x= sc.nextInt();
System.out.print("Enter y: ");
int y= sc.nextInt();

try{
int answer =x/y;
System.out.println("Qoutient: "+ answer);
}catch(ArithmeticException e){
System.out.print("Division by zero is illegal");
}

}
}

You might also like