Main Blog
Our most important blog

Home Page




Posts Tagged ‘Main String’

Mirror the image horizontally?

I tried many time to mirror the image horizontally, but it didn’t appeared quite well. Could anyone help me and check my code please?

import java.awt.Color;

public class TestImage{

public static void mirrorHorizontal(Picture p)
{
int mirrorPoint = p.getHeight() / 2;
Pixel topPixel = null;
Pixel bottomPixel = null;

for (int x = 0; x < p.getHeight(); x++)
{

for (int y = 0; y < mirrorPoint; y++)
{
topPixel = p.getPixel(x,y);
bottomPixel = p.getPixel(p.getHeight() - 1 - y,x);
bottomPixel.setColor(topPixel.getColor());
}
}
}
public static void main(String[] args)
{
String fileName = FileChooser.pickAFile();
Picture pictObj = new Picture(fileName);
pictObj.explore();
mirrorHorizontal(pictObj);
pictObj.explore();
}
}

By: Gag G



1 Comment

Java Recursion Question?

Here’s my code. This little program here prints out “1 2 3″. I don’t understand how though. It looks to me like it would never reach the System.out.print(n+ ” “) since xMethod keeps calling itself before it reaches the print statement until n is no longer greater than 0 and then….the if statement wouldn’t be true to even print it once. If someone can talk me through it, it would help immensely. Thanks.

public class Test{

public static void main(String[] args){
xMethod(3);
}
public static void xMethod (int n){
if(n>0){
xMethod(n-1);
System.out.print(n+ ” “);
}
}
}

By: Fieyr



1 Comment

Help finishing java method?

Hi, I have to write a java method for a collage assignment, I need to be able to print the names and mark of a student from an array, here is what I have so far I just cant finish writing the method.

public class ExamResults
{
public static void main (String []args)
{
int size = Console.readInt(”How Many Students?”);
String [] name = new String [size];
int [] mark = new int [size];
for (int i=0; i {
name[i]= Console.readLine("Enter Name of Student ["+i+"]");
mark[i]= Console.readInt("Enter Mark of Student ["+i+"]");
}
printdetails(name, mark);
System.out.println("");
}

public static void printdetails (String[]n, int[]m)
{
}
}

By: W4lshy



No Comments

Can anyone suggest how to fix this Java program, it keeps on giving me errors?

//Assignment.java
// Program to caculate payroll
// Displays employees name and calculated pay
import java.util.Scanner;
public class Assignment
{
public static void main( String args[] );
{
Scanner input= new Scanner( System.in );

int rate;
int hours;
int weekly;

System.out.println(”_____________”);
System.out.println(”_____________”);
System.out.println(”_____________”);

System.out.print(”program calculation”);
//Calculate weekly pay for employees
System.out.printf(”Enter employees name here”);
String employee_name=input.next();

System.out.print(”Enter employees hourly rate here”);
double rate=input.nextDouble();

System.out.print(”Enter the number of hours the employee worked this week here”);
hours=input.nextDouble();
double weekly= rate*hours;

System.out.printf(”The employee:%d\n”, name);
System.out.printf(”Weekly pay is:%d\n”, weekly);
}
}
I do not want anyone to do it for me. I would just like to know what I am doing wrong. I find this stuff facinating and would like to be able to do it for myself. I am just trying to learn it over the internet and it is a little confusing. Thanks in advance for the assistance.

By: Konux



No Comments

Why doesn’t this work?

Am I even close to making a calculator?

package Assignment4;
import java.util.*;

public class Assignment4 {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
float operand1;
float operand2;

String operator;
String total;

System.out.print(”Enter first operand”);

operand1 = console.nextInt();

System.out.print(”Enter second operand”);

operand2 = console.nextInt();

System.out.print(”Enter operator”);

if (”operator” == “+”)
total = (”operand1 + operand2″);

else if (”operator” == “-”)
total = (”operand1 - operand2″);

else if (”operator” == “*”)
total = (”operand1 * operand2″);

else if (”operator” == “/”)
total = (”operand1 / operand2″);

System.out.println(”The answer is ” + “total”);
}
private static void operator() {

}
}

By: jkherberger



1 Comment

Java Program?

I am creating a java program that is supposed to do some calculation when a particular option is selected from the menu. So far i have done some of the class that i intended to use. However i donot know how to call the class in main so as to execute the program. Please assist by providing examples
class Factorial {
public static void main(String[] args) throws IOException {
int fact; String input;
Int x; try{ InputStreamReader reader =new InputStreamReader(System.in));
BufferedReader input = new BufferedReader(reader);
System.out.println(”enter the number”) input = input.readLine(); fact= new Integer(input).intvalue();while(x!=0 {
fact= fact*x ; System.out.println(”Factorial of ” +input+ “:”);
System.out.print(+fact); x–;}

N.B Wasn’t able to run codes cause computer not working. Feel free to edit class if needs be. Thanks in advance

By: setfree4eva



1 Comment

why doesn’t my polygon draw (java program)?

it makes this teeny tiny line

whats wrong with it, please help!!!!!!!!!!!!1
___________________________________
import gpdraw.*;

public class GraphicPolygon extends RegularPolygons {
private DrawingTool pen;
private SketchPad myPaper;
private double xPosition, yPosition;

// constructors

// Initializes a polygon of numSides sides and sideLength
// length in the superclass. The polygon is centered at
// xPosition = yPosition = 0
public GraphicPolygon(double numSides, double sideLength, double xPosition, double yPosition){

super (numSides, sideLength);
xPosition=0;
yPosition=0;
}

public GraphicPolygon(int numSides, double sideLength, double x, double y){
xPosition = x;
yPosition = y;
myPaper = new SketchPad (500, 500);
pen = new DrawingTool (myPaper);
}

// public methods

// Sets xPosition = x and yPosition = y to correspond to the new
// center of the polygon
public void moveTo(double x, double y){
pen.up();
pen.move(x,y);
}

public double getLength(int sideLength) {
return sideLength;
}

public double getSides (int numSides) {
return numSides;
}

// Draws the polygon about the center point (xPosition, yPosition).
// Uses properties inherited from RegularPolygon to determine the
// number and length of the sides, the radius of the inscribed circle,
// and the vertex angle with which to draw the polygon
public void draw(double sideLength, int numSides){
pen.down();
pen.forward(sideLength);
pen.turn(((numSides - 2)*180)/numSides);
pen.forward(sideLength);
pen.turn(((numSides - 2)*180)/numSides);
pen.forward(sideLength);
pen.turn(((numSides - 2)*180)/numSides);
pen.forward(sideLength);
pen.turn(((numSides - 2)*180)/numSides);
}
}

public class Driver {

public static void main(String[] args) {

GraphicPolygon poly = new GraphicPolygon(4,150, 0, 0);
GraphicPolygon poly2 = new GraphicPolygon(4,150,10,100);
poly2.getSides(4);
poly2.getLength(150);
poly2.moveTo(10,100);
poly2.draw(4,150);
}
}

By: BB



1 Comment

Help compling java file?

When I tries to compile a java file, it says that can not find the main class. See the file is like this-
public class HelloWorld{
public static void main (String[] args) {
System.out.println(”Hello, world!\n”);
}
}

By: Emperor Harsh, the great



No Comments

how can i make the size of the stack open by using inheritance?

import javax.swing.*;
public class StackArray
{
protected E StackItems[];
protected int topItem =-1;
protected final int STACK_SIZE=100;
public StackArray()
{
StackItems=(E[]) new Object[STACK_SIZE];
}
public E push(E item)
{
if(topItem StackItems[++topItem]=item;
return item;
}
public E pop()
{
if(topItem==-1)
return null;
return StackItems[topItem--];
}
public boolean empty()
{
return(topItem==-1);
}
public int search(E target)
{
for(int i=topItem;i>=0;i–)
if(StackItems[i].equals(target))
return topItem-i+1;
return -1;
}
public String toString()
{
String content=new String();
for(int i=0;i<=topItem;i++)
content=new String(content+""+StackItems[i]);
return content;
}
public static void main(String[] args)
{
StackArraymyStack=new StackArray();
myStack.push(1);
myStack.push(2);
myStack.push(2);
myStack.push(4);
System.out.println(myStack.toString());
System.out.println(myStack.search(4));
System.out.println(myStack.search(1));
System.out.println(myStack.search(2));
myStack.pop();
myStack.pop();
System.out.println(myStack.toString());
System.out.println(myStack.search(4));
System.out.println(myStack.search(2));
}
}

By: eng_ sara



1 Comment

Java Program Question?

I have written the following program. I need help with the part 2 where :Using While/IF loops User needs to enter Rectangle or Triangle to find the area of shape or hit X to end the program. Program#2 Calls CalculateAreaOfShapes(my program#1). Any help would be appreciated.

Program#1:

public class CalculateAreaOfShapes
{
double area;
double height;
double width;
double base;

public double calculateAreaOfRectangle(double height, double width)
{
return area = (height * width);

}

public double calculatAreaOfTriangle(double base, double height)
{
return area = (height*base)/2;

}

}

Program#2
public class Test2CalculateAreaOfShapes
{

public static void main(String[] args) // main method
{

CalculateAreaOfShapes A = new CalculateAreaOfShapes();

double tbase = 5.0; //Hard code base & height
double theight = 5.0;

double tarea = A.calculatAreaOfTriangle(tbase, theight);

System.out.println(”Triangle area is “+ tarea);

double twidth = 8.0; //Hard code width & height
double rheight = 7.0;

double rarea = A.calculateAreaOfRectangle(rheight, twidth);

System.out.println(”Rectangle area is “+ rarea);

}
}

By: ART



1 Comment