Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Lab4
- // Problem 1 (using scanner)
- import java.util.Scanner;
- class Problem1{
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- int num1=sc.nextInt();
- char c=sc.next().charAt(0);
- int num2=sc.nextInt();
- if (c=='+'){
- int z=num1+num2;
- System.out.print(z);
- }
- if (c=='-'){
- int z=num1-num2;
- System.out.print(z);
- }
- if (c=='*'){
- int z=num1*num2;
- System.out.print(z);
- }
- if (c=='/'){
- double z=(double)num1/num2;
- System.out.print(z);
- }
- if (c=='%'){
- int z=num1%num2;
- System.out.print(z);
- }
- }
- }
- // Problem 1 (using command-line arguments)
- public class command{
- public static void main(String args[]){
- int z=Integer.parseInt(args[0]);
- char c=args[1].charAt(0);
- int y=Integer.parseInt(args[2]);
- if (c=='+'){
- System.out.println(y+z);
- }
- if (c=='-'){
- System.out.println(y-z);
- }
- if (c=='*'){
- System.out.println(y*z);
- }
- if (c=='/'){
- float x=(float)z/(float)y;
- System.out.println(x);
- }
- if (c=='%'){
- System.out.println(z%y);
- }
- }
- }
- // Problem 2
- import java.util.*;
- class Problem2{
- static boolean ctain(String s,String t){
- int max=s.length()-t.length();
- int n =t.length();
- for(int i=0;i<=max;i++){
- for(int j=0;j<n;j++){
- if(s.charAt(i+j)!=t.charAt(j)){
- break;
- }
- else if(j==n-1){
- return true;
- }
- }
- }
- return false;
- }
- static String ccat(String s, String t){
- String u=s+t;
- return u;
- }
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- String S=sc.nextLine();
- String T=sc.nextLine();
- System.out.println(ccat(S,T));
- if (ctain(S,T)==true){
- System.out.println("The second string is a substring of the first string");
- }
- else {
- System.out.println("The second string is not a substring of the first string");
- }
- }
- }
- // Problem 3
- import java.util.*;
- public class Problem3{
- static double findDistance(double x, double y, double a, double b){
- double z=Math.pow((x-a)*(x-a)+(y-b)*(y-b),0.5);
- return z;
- }
- public static void main(String args[]){
- Point object1=new Point();
- Point object2=new Point();
- System.out.println("Point 1 is : (" + object1.first + ", " + object1.second + ")");
- System.out.println("Point 2 is : (" + object2.first + ", " + object2.second + ")");
- System.out.println("The distance between the two points is: " + findDistance(object1.first, object1.second, object2.first, object2.second));
- }
- }
- class Point{
- double z=Math.random()*100;
- double first=z;
- double y=Math.random()*100;
- double second=y;
- }
- // Problem 4 (by atish)
- import java.util.*;
- public class Program4{
- public static void main(String[] args) {
- Scanner sc= new Scanner(System.in);
- timestamp one= new timestamp("08:20:46");
- String two=sc.nextLine();
- timestamp result=timestamp.add(one,two);
- timestamp.print(result);
- }
- }
- class timestamp{
- int hours;
- int minutes;
- int seconds;
- timestamp(){
- hours=0;
- minutes=0;
- seconds=0;
- }
- timestamp(String s){
- char h1=s.charAt(0); int h1i=h1-48;
- char h2=s.charAt(1); int h2i=h2-48;
- char m1=s.charAt(3); int m1i=m1-48;
- char m2=s.charAt(4); int m2i=m2-48;
- char s1=s.charAt(6); int s1i=s1-48;
- char s2=s.charAt(7); int s2i=s2-48;
- this.hours=h1i*10+h2i;
- this.minutes=m1i*10+m2i;
- this.seconds=s1i*10+s2i;
- }
- static timestamp add(timestamp in,String b){
- timestamp add= new timestamp(b);
- timestamp out=new timestamp();
- out.seconds=(in.seconds+add.seconds)%60;
- out.minutes=(in.minutes+add.minutes+(in.seconds+add.seconds)/60)%60;
- out.hours=(in.hours+add.hours+(in.minutes+add.minutes+(in.seconds+add.seconds)/60)/60)%24;
- return out;
- }
- static void print(timestamp t){
- System.out.print(t.hours);
- System.out.print(":");
- System.out.print(t.minutes);
- System.out.print(":");
- System.out.println(t.seconds);
- }
- }
- // Problem 5 (using Math.random())
- import java.util.*;
- public class command2{
- public static void main(String args[]){
- int x=(int)(Math.random()*100);
- int y=(int)(Math.random()*100);
- int z=(int)(Math.random()*100);
- Rectangle a=new Rectangle();
- System.out.println("length: " + a.length +" breadth: " + a.width);
- System.out.println("area: " + Rectangle.area(a));
- Rectangle b=new Rectangle(x);
- System.out.println("length: " + b.length +" breadth: " + b.width);
- System.out.println("area: " + Rectangle.area(b));
- Rectangle c=new Rectangle(y,z);
- System.out.println("length: " + c.length +" breadth: " + c.width);
- System.out.println("area: " + Rectangle.area(c));
- }
- }
- class Rectangle{
- int width;
- int length;
- Rectangle(){
- this.length=0;
- this.width=0;
- }
- Rectangle(int a){
- this.length=a;
- this.width=a;
- }
- Rectangle(int a, int b){
- this.length=a;
- this.width=b;
- }
- static int area(Rectangle a){
- return a.length*a.width;
- }
- }
- // Problem 5 (by atish)
- import java.util.*;
- class Rectangle{
- int length;
- int width;
- Rectangle(){
- this.length=0;
- this.width=0;
- }
- Rectangle(int a){
- this.length=a;
- this.width=a;
- }
- Rectangle(int a,int b){
- this.length=a;
- this.width=b;
- }
- static int calculate(Rectangle a){
- return a.length*a.width;
- }
- }
- public class Problem5{
- public static void main(String[] args){
- Rectangle a= new Rectangle();
- System.out.println(Rectangle.calculate(a));
- Rectangle b= new Rectangle(5);
- System.out.println(Rectangle.calculate(b));
- Rectangle c= new Rectangle(5,7);
- System.out.println(Rectangle.calculate(c));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement